class Cassie::Support::SystemCommand

Attributes

args[R]
binary[R]
command[R]
duration[R]
output[R]
status[R]

Public Class Methods

exist?(name) click to toggle source

Indicates whether a binary exists in the current user's PATH @param [String, Symbol] name the name of the command to search for @return [Boolean] true if the binary could be found

# File lib/cassie/support/system_command.rb, line 9
def self.exist?(name)
  !!which(name)
end
new(*cmd) click to toggle source

Creates a new SystemCommand object that has not yet been executed.

@overload initialize(command)

@param [#to_s] the command to execute

@overload initialize(binary, [args]=[])

@param [#to_s] binary the binary to be called
@param [Array<#to_s>] args Arguments to be passed to the binary

@overload initialize(binary, arg, …)

@param [#to_s] binary the binary to be called
@param [#to_s, Array<#to_s>] arg Argument(s) to be passed to the binary
@param [#to_s, Array<#to_s>] ... more argument(s) to be passed to the binary

@example command string

cmd = SystemCommand.new("git reset --hard HEAD")
cmd.binary
#=> "git"
cmd.args
#=> ["reset", "--hard", "HEAD"]

@example binary and arguments strings

cmd = SystemCommand.new("git", "reset --hard HEAD")
cmd.binary
#=> "git"
cmd.args
#=> ["reset", "--hard", "HEAD"]

@example binary and arguments string with splat

cmd = SystemCommand.new("git", "reset", "--hard HEAD")
cmd.binary
#=> "git"
cmd.args
#=> ["reset", "--hard", "HEAD"]

@example binary with arguments array

cmd = SystemCommand.new("git", ["reset", "--hard", "HEAD"])
cmd.binary
#=> "git"
cmd.args
#=> ["reset", "--hard", "HEAD"]

@example array

cmd = SystemCommand.new(["git", "reset", "--hard", "HEAD"])
cmd.binary
#=> "git"
cmd.args
#=> ["reset", "--hard", "HEAD"]
# File lib/cassie/support/system_command.rb, line 77
def initialize(*cmd)
  @args = []
  cmd.flatten.each{|a| @args += a.to_s.split(" ")}

  @command = args.join(" ")
  @command = command + " 2>&1" unless command =~ / > /

  @binary = @args.shift
end
which(name) click to toggle source

Find the path to the executable file, using the current user's PATH @param [String, Symbol] name the name of the command to search for @return [String, nil] the fully qualified path

# File lib/cassie/support/system_command.rb, line 16
def self.which(name)
  # windows support
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{name}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end

Public Instance Methods

completed?() click to toggle source

@return [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.
# File lib/cassie/support/system_command.rb, line 138
def completed?
  return false unless run?
  status.exited? && @status.success? #status.success is NOT the exit code == 0!
end
exist?() click to toggle source
# File lib/cassie/support/system_command.rb, line 87
def exist?
  self.class.exist?(binary)
end
exitcode() click to toggle source

Runs the command if it hasn't been run yet. @return [Fixnum] the exit code for the command.

# File lib/cassie/support/system_command.rb, line 125
def exitcode
  status.exitstatus
end
failure_message() click to toggle source
# File lib/cassie/support/system_command.rb, line 143
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
run() click to toggle source

Runs the command @return [Boolean] true if execution completed without crashing

# File lib/cassie/support/system_command.rb, line 97
def run
  t1=Time.now

  IO.popen(command) do |io|
    @status=Process.waitpid2(io.pid)[1]
    @output=io.read.sub(/\n\z/, "")
  end

  @duration=Time.now-t1
  completed?
end
run?() click to toggle source

@return [Boolean] false if the command hasn't been run yet

# File lib/cassie/support/system_command.rb, line 119
def run?
  !!@duration
end
succeed() click to toggle source

Runs the command, expecting an exit status of 0 @return [Boolean] true if execution completed without crashing @raise [RuntimeError] if program was not run successfully

# File lib/cassie/support/system_command.rb, line 112
def succeed
  fail unless run && success?

  true
end
success?() click to toggle source

@return [Boolean] true if command has been run, and exited with status of 0,

otherwise returns false.
# File lib/cassie/support/system_command.rb, line 131
def success?
  return false unless run?
  exitcode == 0
end
which() click to toggle source
# File lib/cassie/support/system_command.rb, line 91
def which
  self.class.which(binary)
end

Protected Instance Methods

fail() click to toggle source

raise a Runtime error with a failure message specific to the command

# File lib/cassie/support/system_command.rb, line 156
def fail
  raise RuntimeError.new(failure_message)
end
red(message) click to toggle source
# File lib/cassie/support/system_command.rb, line 160
def red(message)
  "\e[1m\e[31m#{message}\e[0m\e[22m"
end