This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
/
command_line.rb
147 lines (123 loc) · 3.44 KB
/
command_line.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# coding: UTF-8
module Cocaine
class CommandLine
class << self
attr_accessor :logger, :runner
def path
@supplemental_path
end
def path=(supplemental_path)
@supplemental_path = supplemental_path
@supplemental_environment ||= {}
@supplemental_environment['PATH'] = (Array(supplemental_path) + [ENV['PATH']]).join(File::PATH_SEPARATOR)
end
def environment
@supplemental_environment ||= {}
end
def runner
@runner || best_runner
end
def fake!
@runner = FakeRunner.new
end
def unfake!
@runner = nil
end
def java?
RUBY_PLATFORM =~ /java/
end
private
def best_runner
[PosixRunner, ProcessRunner, BackticksRunner].detect do |runner|
runner.supported?
end.new
end
end
@environment = {}
attr_reader :exit_status, :runner
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
def command(interpolations = {})
cmd = []
cmd << @binary
cmd << interpolate(@params, interpolations)
cmd << bit_bucket if @swallow_stderr
cmd.join(" ").strip
end
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
def unix?
RbConfig::CONFIG['host_os'] !~ /mswin|mingw/
end
private
def colored(text, ansi_color = "\e[32m")
if @logger && @logger.respond_to?(:tty?) && @logger.tty?
"#{ansi_color}#{text}\e[0m"
else
text
end
end
def log(text)
if @logger
@logger.info(text)
end
end
def execute(command)
runner.call(command, environment)
end
def environment
self.class.environment.merge(@environment)
end
def interpolate(pattern, interpolations)
interpolations.inject(pattern) do |command_string, (key, value)|
command_string.gsub(/:\{?#{key}\}?/) { shell_quote(value) }
end
end
def shell_quote(string)
return "" if string.nil?
if unix?
if string.empty?
"''"
else
string.split("'").map{|m| "'#{m}'" }.join("\\'")
end
else
%{"#{string}"}
end
end
def bit_bucket
unix? ? "2>/dev/null" : "2>NUL"
end
end
end