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/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, 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.



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

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.



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

def exit_status
  @exit_status
end

#runnerObject (readonly)

Returns the value of attribute runner.



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

def runner
  @runner
end

Class Method Details

.environmentObject



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

def environment
  @supplemental_environment ||= {}
end

.fake!Object



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

def fake!
  @runner = FakeRunner.new
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

.posix_spawn_available?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/cocaine/command_line.rb', line 18

def posix_spawn_available?
  @posix_spawn_available ||= begin
    require 'posix/spawn'
    true
  rescue LoadError => e
    false
  end
end

.unfake!Object



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

def unfake!
  @runner = nil
end

Instance Method Details

#command(interpolations = {}) ⇒ Object



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

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

#run(interpolations = {}) ⇒ Object



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

def run(interpolations = {})
  output = ''
  begin
    full_command = command(interpolations)
    log("#{colored("Command")} :: #{full_command}")
    output = execute(full_command)
  rescue Errno::ENOENT
    raise Cocaine::CommandNotFoundError
  ensure
    @exit_status = $?.exitstatus
  end
  if $?.exitstatus == 127
    raise Cocaine::CommandNotFoundError
  end
  unless @expected_outcodes.include?($?.exitstatus)
    message = [
      "Command '#{full_command}' returned #{$?.exitstatus}. Expected #{@expected_outcodes.join(", ")}",
      "Here is the command output:\n",
      output
    ].join("\n")
    raise Cocaine::ExitStatusError, message
  end
  output
end

#unix?Boolean

Returns:

  • (Boolean)


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

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