Class: Cocaine::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/cocaine/command_line.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, 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.



44
45
46
47
48
49
50
51
52
53
# File 'lib/cocaine/command_line.rb', line 44

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.



42
43
44
# File 'lib/cocaine/command_line.rb', line 42

def exit_status
  @exit_status
end

#runnerObject (readonly)

Returns the value of attribute runner.



42
43
44
# File 'lib/cocaine/command_line.rb', line 42

def runner
  @runner
end

Class Method Details

.environmentObject



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

def environment
  @supplemental_environment ||= {}
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

Instance Method Details

#commandObject



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

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

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cocaine/command_line.rb', line 63

def run
  output = ''
  begin
    @logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
    output = execute(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)


82
83
84
# File 'lib/cocaine/command_line.rb', line 82

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