Class: Cocaine::CommandLine

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

Defined Under Namespace

Classes: BackticksRunner, FakeRunner, 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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cocaine/command_line.rb', line 55

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/cocaine/command_line.rb', line 6

def logger
  @logger
end

.runnerObject

Returns the value of attribute runner



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

def runner
  @runner
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status



53
54
55
# File 'lib/cocaine/command_line.rb', line 53

def exit_status
  @exit_status
end

#runnerObject (readonly)

Returns the value of attribute runner



53
54
55
# File 'lib/cocaine/command_line.rb', line 53

def runner
  @runner
end

Class Method Details

.environmentObject



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

def environment
  @supplemental_environment ||= {}
end

.fake!Object



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

def fake!
  @runner = FakeRunner.new
end

.java?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/cocaine/command_line.rb', line 38

def java?
  RUBY_PLATFORM =~ /java/
end

.pathObject



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

def path
  @supplemental_path
end

.path=(supplemental_path) ⇒ Object



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

def path=(supplemental_path)
  @supplemental_path = supplemental_path
  @supplemental_environment ||= {}
  @supplemental_environment['PATH'] = (Array(supplemental_path) + [ENV['PATH']]).join(File::PATH_SEPARATOR)
end

.runner_optionsObject



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

def runner_options
  @default_runner_options ||= {}
end

.unfake!Object



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

def unfake!
  @runner = nil
end

Instance Method Details

#command(interpolations = {}) ⇒ Object



67
68
69
70
71
# File 'lib/cocaine/command_line.rb', line 67

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

#run(interpolations = {}) ⇒ Object



73
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/cocaine/command_line.rb', line 73

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

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

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

#unix?Boolean

Returns:

  • (Boolean)


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

def unix?
  RbConfig::CONFIG['host_os'] !~ /mswin|mingw/
end