Class: Cassie::Support::SystemCommand
- Defined in:
- lib/cassie/support/system_command.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#binary ⇒ Object
readonly
Returns the value of attribute binary.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#completed? ⇒ Boolean
True if the command completed execution and success bits were set, regardless of the exit status code.
-
#exitcode ⇒ Fixnum
Runs the command if it hasn’t been run yet.
- #failure_message ⇒ Object
-
#initialize(binary, args = []) ⇒ SystemCommand
constructor
When a block is given, the command runs before yielding.
-
#run ⇒ Boolean
Runs the command.
-
#run? ⇒ Boolean
False if the command hasn’t been run yet.
-
#succeed ⇒ Boolean
Runs the command, expecting an exit status of 0.
-
#success? ⇒ Boolean
True if command has been run, and exited with status of 0, otherwise returns false.
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def args @args end |
#binary ⇒ Object (readonly)
Returns the value of attribute binary.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def binary @binary end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def command @command end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def duration @duration end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
4 5 6 |
# File 'lib/cassie/support/system_command.rb', line 4 def output @output end |
#status ⇒ Object (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 |
#exitcode ⇒ Fixnum
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_message ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/cassie/support/system_command.rb', line 67 def 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 |
#run ⇒ Boolean
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 |
#succeed ⇒ Boolean
Runs the command, expecting an exit status of 0
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 |