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/process_runner.rb,
lib/terrapin/command_line/runners/backticks_runner.rb

Defined Under Namespace

Classes: BackticksRunner, FakeRunner, MultiPipe, Output, PopenRunner, 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
62
63
64
65
66
# File 'lib/terrapin/command_line.rb', line 51

def initialize(binary, params = "", options = {})
  if options.nil?
    raise ArgumentError, "3rd argument to CommandLine.new should be a" \
      "hash of values that will be interpolated into the command line"
  end

  @options = options.dup
  @binary = binary.dup
  @params = params.to_s.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



68
69
70
71
72
# File 'lib/terrapin/command_line.rb', line 68

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

#command_error_outputObject



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

def command_error_output
  output.error_output
end

#command_outputObject



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

def command_output
  output.output
end

#outputObject



109
110
111
# File 'lib/terrapin/command_line.rb', line 109

def output
  @output || Output.new
end

#run(interpolations = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/terrapin/command_line.rb', line 74

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