Class: Cassie::Support::SystemCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/cassie/support/system_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary, args = []) ⇒ SystemCommand

When a block is given, the command runs before yielding



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cassie/support/system_command.rb', line 7

def initialize(binary, args=[])
  @binary = binary
  @args = args
  @command = (Array(binary) + args).join(" ")
  @command = command + " 2>&1" unless command =~ / > /

  if block_given?
    run
    yield self
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/cassie/support/system_command.rb', line 4

def args
  @args
end

#binaryObject (readonly)

Returns the value of attribute binary.



4
5
6
# File 'lib/cassie/support/system_command.rb', line 4

def binary
  @binary
end

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/cassie/support/system_command.rb', line 4

def command
  @command
end

#durationObject (readonly)

Returns the value of attribute duration.



4
5
6
# File 'lib/cassie/support/system_command.rb', line 4

def duration
  @duration
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/cassie/support/system_command.rb', line 4

def output
  @output
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/cassie/support/system_command.rb', line 4

def status
  @status
end

Instance Method Details

#completed?Boolean

Returns true if the command completed execution and success bits were set, regardless of the exit status code. false if the command hasn’t been executed, failed to exit, or crashed.

Returns:

  • (Boolean)

    true if the command completed execution and success bits were set, regardless of the exit status code. false if the command hasn’t been executed, failed to exit, or crashed.



62
63
64
65
# File 'lib/cassie/support/system_command.rb', line 62

def completed?
  return false unless run?
  status.exited? && @status.success? #status.success is NOT the exit code == 0!
end

#exitcodeFixnum

Runs the command if it hasn’t been run yet.

Returns:

  • (Fixnum)

    the exit code for the command.



49
50
51
# File 'lib/cassie/support/system_command.rb', line 49

def exitcode
  status.exitstatus
end

#failure_messageObject



67
68
69
70
71
72
73
74
# File 'lib/cassie/support/system_command.rb', line 67

def failure_message
  msg = "---------------------\n"
  msg << red(output)
  msg << "---------------------\n"
  msg << "Failed to execute `#{command}`:\n"
  msg << "\tPlease check the output above for any errors and make sure that `#{binary}` is installed in your PATH with proper permissions."
  msg
end

#runBoolean

Runs the command

Returns:

  • (Boolean)

    true if execution completed without crashing



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cassie/support/system_command.rb', line 21

def run
  t1=Time.now

  IO.popen(command) do |io|
    @output=io.read
    @status=Process.waitpid2(io.pid)[1]
  end

  @duration=Time.now-t1
  completed?
end

#run?Boolean

Returns false if the command hasn’t been run yet.

Returns:

  • (Boolean)

    false if the command hasn’t been run yet



43
44
45
# File 'lib/cassie/support/system_command.rb', line 43

def run?
  !!@duration
end

#succeedBoolean

Runs the command, expecting an exit status of 0

Returns:

  • (Boolean)

    true if execution completed without crashing

Raises:

  • (RuntimeError)

    if program was not run successfully



36
37
38
39
40
# File 'lib/cassie/support/system_command.rb', line 36

def succeed
  fail unless run && success?

  true
end

#success?Boolean

Returns true if command has been run, and exited with status of 0, otherwise returns false.

Returns:

  • (Boolean)

    true if command has been run, and exited with status of 0, otherwise returns false.



55
56
57
58
# File 'lib/cassie/support/system_command.rb', line 55

def success?
  return false unless run?
  exitcode == 0
end