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.



51
52
53
54
55
56
57
58
59
60
# File 'lib/cocaine/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) || {}
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.



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

def exit_status
  @exit_status
end

#runnerObject (readonly)

Returns the value of attribute runner.



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

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



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

def fake!
  @runner = FakeRunner.new
end

.java?Boolean

Returns:

  • (Boolean)


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

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

.unfake!Object



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

def unfake!
  @runner = nil
end

Instance Method Details

#command(interpolations = {}) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/cocaine/command_line.rb', line 62

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

#run(interpolations = {}) ⇒ Object



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

def run(interpolations = {})
  output = ''
  @exit_status = nil
  begin
    full_command = command(interpolations)
    log("#{colored("Command")} :: #{full_command}")
    output = execute(full_command)
  rescue Errno::ENOENT
    raise Cocaine::CommandNotFoundError
  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)


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

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