class RunIt

Constants

VERSION

Version

Attributes

cmd[RW]
cmd

set/get the command to execute

error[R]
error

get the error messages from the command

input[RW]
input

set/get the input for the command

output[R]
output

get the output from the command

result[R]
result

the result of the command as a Process::Status object

Public Class Methods

mock!() click to toggle source

Class method to turn on mocking

# File lib/RunIt.rb, line 54
def self.mock!
  @@mock = true
end
mock?() click to toggle source

Class function to tell if mocking is turned on

# File lib/RunIt.rb, line 49
def self.mock?
  @@mock
end
new(cmd, input=nil) click to toggle source

Initialize the RunIt object

cmd

(required) [String] - the full command line to execute

input

(optional) [String] - input to the command, if any

# File lib/RunIt.rb, line 67
def initialize(cmd, input=nil)
  self.cmd = cmd
  self.input = input
  @result = nil
  @@mock = false
end
unmock!() click to toggle source

Class method to turn off mocking

# File lib/RunIt.rb, line 59
def self.unmock!
  @@mock = false
end

Public Instance Methods

exitstatus() click to toggle source

The exit status of the command

# File lib/RunIt.rb, line 125
def exitstatus
  self.result.exitstatus
end
mock?() click to toggle source

Instance method to tell if mocking is turned on

# File lib/RunIt.rb, line 130
def mock?
  @@mock
end
run() click to toggle source

Execute the specified command

If mocking is on, only return the command as the output, and set success to true

# File lib/RunIt.rb, line 77
def run

  if @@mock

    @result = OpenStruct.new(:success? => true, :exitstatus => 0)
    @output = "Command entered: #{self.cmd}"
    @error  = ''
    return true

  else

    @output = ''
    @error  = ''
    @result = nil

    begin

      Open3.popen3(self.cmd) do |stdin, stdout, stderr, wait|
        stdin.puts self.input unless self.input.nil?
        stdin.close
        until stdout.eof?
          @output <<  stdout.gets
        end
        until stderr.eof?
          @error <<  stderr.gets
        end
        @result = wait.value
      end

    rescue Exception => e

      @result = OpenStruct.new(:success? => false, :exitstatus => -1)
      @error << "#{cmd} raised an error: #{e.class}:#{e}"

    end

    self.result.success?

  end

end
success?() click to toggle source

Whether the command completed successfully

# File lib/RunIt.rb, line 120
def success?
  self.result.success?
end