Class: Perfume::Shell::Exec

Inherits:
Base
  • Object
show all
Defined in:
lib/perfume/shell/exec.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#before, #defaults, #log, #to_s

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



26
27
28
# File 'lib/perfume/shell/exec.rb', line 26

def output
  @output
end

#rcObject (readonly)

Returns the value of attribute rc.



26
27
28
# File 'lib/perfume/shell/exec.rb', line 26

def rc
  @rc
end

Instance Method Details

#call(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# 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?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/perfume/shell/exec.rb', line 33

def fail_on_error?
  @fail_on_error
end

#initObject



28
29
30
31
# File 'lib/perfume/shell/exec.rb', line 28

def init
  @__done = []
  @fail_on_error = false if @fail_on_error.nil?
end