Class: Cocaine::CommandLine
- Inherits:
-
Object
- Object
- Cocaine::CommandLine
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.
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/cocaine/command_line.rb', line 32
def initialize(binary, params = "", options = {})
@binary = binary.dup
@params = params.dup
@options = options.dup
@logger = @options.delete(:logger) || self.class.logger
@swallow_stderr = @options.delete(:swallow_stderr)
@expected_outcodes = @options.delete(:expected_outcodes)
@expected_outcodes ||= [0]
@runner = best_runner
end
|
Class Attribute Details
.logger ⇒ Object
Returns the value of attribute logger.
4
5
6
|
# File 'lib/cocaine/command_line.rb', line 4
def logger
@logger
end
|
Instance Attribute Details
#exit_status ⇒ Object
Returns the value of attribute exit_status.
30
31
32
|
# File 'lib/cocaine/command_line.rb', line 30
def exit_status
@exit_status
end
|
#runner ⇒ Object
Returns the value of attribute runner.
30
31
32
|
# File 'lib/cocaine/command_line.rb', line 30
def runner
@runner
end
|
Class Method Details
.environment ⇒ Object
24
25
26
|
# File 'lib/cocaine/command_line.rb', line 24
def environment
@supplemental_environment ||= {}
end
|
.path ⇒ Object
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
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
#command ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/cocaine/command_line.rb', line 43
def command
cmd = []
cmd << @binary
cmd << interpolate(@params, @options)
cmd << bit_bucket if @swallow_stderr
cmd.join(" ").strip
end
|
#run ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/cocaine/command_line.rb', line 51
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
70
71
72
|
# File 'lib/cocaine/command_line.rb', line 70
def unix?
(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/).nil?
end
|