Class: Terrapin::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/terrapin/command_line.rb,
lib/terrapin/command_line/multi_pipe.rb,
lib/terrapin/command_line/runners/fake_runner.rb,
lib/terrapin/command_line/runners/popen_runner.rb,
lib/terrapin/command_line/runners/posix_runner.rb,
lib/terrapin/command_line/runners/process_runner.rb,
lib/terrapin/command_line/runners/backticks_runner.rb

Defined Under Namespace

Classes: BackticksRunner, FakeRunner, MultiPipe, Output, PopenRunner, PosixRunner, ProcessRunner

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary, params = "", options = {}) ⇒ CommandLine

Returns a new instance of CommandLine.


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/terrapin/command_line.rb', line 51

def initialize(binary, params = "", options = {})
  @binary            = binary.dup
  @params            = params.dup
  @options           = options.dup
  @runner            = @options.delete(:runner) || self.class.runner
  @logger            = @options.delete(:logger) || self.class.logger
  @swallow_stderr    = @options.delete(:swallow_stderr)
  @expected_outcodes = @options.delete(:expected_outcodes) || [0]
  @environment       = @options.delete(:environment) || {}
  @runner_options    = @options.delete(:runner_options) || {}
end

Class Attribute Details

.loggerObject

Returns the value of attribute logger


6
7
8
# File 'lib/terrapin/command_line.rb', line 6

def logger
  @logger
end

.runnerObject

Returns the value of attribute runner


6
7
8
# File 'lib/terrapin/command_line.rb', line 6

def runner
  @runner
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status


49
50
51
# File 'lib/terrapin/command_line.rb', line 49

def exit_status
  @exit_status
end

#runnerObject (readonly)

Returns the value of attribute runner


49
50
51
# File 'lib/terrapin/command_line.rb', line 49

def runner
  @runner
end

Class Method Details

.environmentObject


18
19
20
# File 'lib/terrapin/command_line.rb', line 18

def environment
  @supplemental_environment ||= {}
end

.fake!Object


30
31
32
# File 'lib/terrapin/command_line.rb', line 30

def fake!
  @runner = FakeRunner.new
end

.pathObject


8
9
10
# File 'lib/terrapin/command_line.rb', line 8

def path
  @supplemental_path
end

.path=(supplemental_path) ⇒ Object


12
13
14
15
16
# File 'lib/terrapin/command_line.rb', line 12

def path=(supplemental_path)
  @supplemental_path = Array(supplemental_path).
    flatten.
    join(OS.path_separator)
end

.runner_optionsObject


26
27
28
# File 'lib/terrapin/command_line.rb', line 26

def runner_options
  @default_runner_options ||= {}
end

.unfake!Object


34
35
36
# File 'lib/terrapin/command_line.rb', line 34

def unfake!
  @runner = nil
end

Instance Method Details

#command(interpolations = {}) ⇒ Object


63
64
65
66
67
# File 'lib/terrapin/command_line.rb', line 63

def command(interpolations = {})
  cmd = [path_prefix, @binary, interpolate(@params, interpolations)]
  cmd << bit_bucket if @swallow_stderr
  cmd.join(" ").strip
end

#command_error_outputObject


100
101
102
# File 'lib/terrapin/command_line.rb', line 100

def command_error_output
  output.error_output
end

#command_outputObject


96
97
98
# File 'lib/terrapin/command_line.rb', line 96

def command_output
  output.output
end

#outputObject


104
105
106
# File 'lib/terrapin/command_line.rb', line 104

def output
  @output || Output.new
end

#run(interpolations = {}) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/terrapin/command_line.rb', line 69

def run(interpolations = {})
  @exit_status = nil
  begin
    full_command = command(interpolations)
    log("#{colored("Command")} :: #{full_command}")
    @output = execute(full_command)
  rescue Errno::ENOENT => e
    raise Terrapin::CommandNotFoundError, e.message
  ensure
    @exit_status = $?.respond_to?(:exitstatus) ? $?.exitstatus : 0
  end

  if @exit_status == 127
    raise Terrapin::CommandNotFoundError
  end

  unless @expected_outcodes.include?(@exit_status)
    message = [
      "Command '#{full_command}' returned #{@exit_status}. Expected #{@expected_outcodes.join(", ")}",
      "Here is the command output: STDOUT:\n", command_output,
      "\nSTDERR:\n", command_error_output
    ].join("\n")
    raise Terrapin::ExitStatusError, message
  end
  command_output
end