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



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.



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



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



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

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



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

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