Class: Cassie::Support::SystemCommand

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ SystemCommand #initialize(binary, [args][]) ⇒ SystemCommand #initialize(binary, arg, ...) ⇒ SystemCommand

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

Examples:

command string

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

binary and arguments strings

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

binary and arguments string with splat

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

binary with arguments array

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

array

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

Overloads:

  • #initialize(command) ⇒ SystemCommand

    Parameters:

    • the (#to_s)

      command to execute

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

    Parameters:

    • binary (#to_s)

      the binary to be called

    • args (Array<#to_s>)

      Arguments to be passed to the binary

  • #initialize(binary, arg, ...) ⇒ SystemCommand

    Parameters:

    • binary (#to_s)

      the binary to be called

    • arg (#to_s, Array<#to_s>)

      Argument(s) to be passed to the binary

    • ... (#to_s, Array<#to_s>)

      more argument(s) to be passed to the binary


77
78
79
80
81
82
83
84
85
# 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

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

Class Method Details

.exist?(name) ⇒ Boolean

Indicates whether a binary exists in the current user’s PATH

Parameters:

  • name (String, Symbol)

    the name of the command to search for

Returns:

  • (Boolean)

    true if the binary could be found


9
10
11
# File 'lib/cassie/support/system_command.rb', line 9

def self.exist?(name)
  !!which(name)
end

.which(name) ⇒ String?

Find the path to the executable file, using the current user’s PATH

Parameters:

  • name (String, Symbol)

    the name of the command to search for

Returns:

  • (String, nil)

    the fully qualified path


16
17
18
19
20
21
22
23
24
25
26
# 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

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.


138
139
140
141
# 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?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/cassie/support/system_command.rb', line 87

def exist?
  self.class.exist?(binary)
end

#exitcodeFixnum

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

Returns:

  • (Fixnum)

    the exit code for the command.


125
126
127
# File 'lib/cassie/support/system_command.rb', line 125

def exitcode
  status.exitstatus
end

#failure_messageObject


143
144
145
146
147
148
149
150
# 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

#runBoolean

Runs the command

Returns:

  • (Boolean)

    true if execution completed without crashing


97
98
99
100
101
102
103
104
105
106
107
# 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?Boolean

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

Returns:

  • (Boolean)

    false if the command hasn’t been run yet


119
120
121
# File 'lib/cassie/support/system_command.rb', line 119

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


112
113
114
115
116
# File 'lib/cassie/support/system_command.rb', line 112

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.


131
132
133
134
# File 'lib/cassie/support/system_command.rb', line 131

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

#whichObject


91
92
93
# File 'lib/cassie/support/system_command.rb', line 91

def which
  self.class.which(binary)
end