class Perfume::Shell::Exec

Public: Go for Exec when you need to obtain standard output of the command. It uses the %x[] call under the hood. Note that it doesn’t raise errors when return status is different than 0. Same as with Perfume::Shell::SystemCall, you should go for inheriting from this class:

class GitDirtyTreeValidation < Perfume::Shell::Exec
  Error = Class.new(StandardError)

  def cmd
    'git status -s'
  end

  def fail_on_error?
    true
  end

  def handle
    raise Error, "Uncommited changes detected!" unless output.strip.empty?
  end
end

Attributes

output[R]
rc[R]

Public Instance Methods

call() { |result| ... } click to toggle source
# File lib/perfume/shell/exec.rb, line 37
def call(&block)
  Dir.chdir(@root.to_s) do
    before
    log.debug("Executing shell command and waiting for result", cmd: cmd, root: @root.to_s)
    @output, @rc = [ %x[#{cmd} 2>&1], $?.to_i ]
    fail! if fail_on_error? and @rc != 0
    result = respond_to?(:handle) ? handle : [ @output, @rc ]
    yield result if block_given?
    return result
  end
end
fail_on_error?() click to toggle source
# File lib/perfume/shell/exec.rb, line 33
def fail_on_error?
  @fail_on_error
end
init() click to toggle source
# File lib/perfume/shell/exec.rb, line 28
def init
  @__done = []
  @fail_on_error = false if @fail_on_error.nil?
end

Protected Instance Methods

fail!(*) click to toggle source
Calls superclass method Perfume::Shell::Base#fail!
# File lib/perfume/shell/exec.rb, line 51
def fail!(*)
  $stderr.write(@output)
  super
end