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.



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

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.



4
5
6
# File 'lib/cocaine/command_line.rb', line 4

def logger
  @logger
end

.runnerObject

Returns the value of attribute runner.



4
5
6
# File 'lib/cocaine/command_line.rb', line 4

def runner
  @runner
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



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

def exit_status
  @exit_status
end

#runnerObject (readonly)

Returns the value of attribute runner.



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

def runner
  @runner
end

Class Method Details

.environmentObject



24
25
26
# File 'lib/cocaine/command_line.rb', line 24

def environment
  @supplemental_environment ||= {}
end

.fake!Object



32
33
34
# File 'lib/cocaine/command_line.rb', line 32

def fake!
  @runner = FakeRunner.new
end

.pathObject



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

def path
  @supplemental_path
end

.path=(supplemental_path) ⇒ Object



9
10
11
12
13
# File 'lib/cocaine/command_line.rb', line 9

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

.posix_spawn_available?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/cocaine/command_line.rb', line 15

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

.unfake!Object



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

def unfake!
  @runner = nil
end

Instance Method Details

#command(interpolations = {}) ⇒ Object



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

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

#run(interpolations = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cocaine/command_line.rb', line 71

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)
    raise Cocaine::ExitStatusError, "Command '#{command}' returned #{$?.exitstatus}. Expected #{@expected_outcodes.join(", ")}"
  end
  output
end

#unix?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/cocaine/command_line.rb', line 91

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