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
|
# File 'lib/retag/utils.rb', line 7
def cmd!(cmd, capture: false, logger: $logger)
$cmdnum ||= 0
$cmdnum += 1
tag = $cmdnum.to_s.rjust(3, '0')
output = ''
logger.tagged("CMD#{tag}".colorize(:light_black)) { logger.debug cmd.colorize(:light_black) }
code = Open3.popen3(cmd) do |stdin, stdout, stderr, thr|
stdin.close_write
files = [stdout, stderr]
until files.empty?
ready = IO.select(files)
readable = ready[0]
readable.each do |f|
begin
line = f.readline
if f == stderr
logger.tagged("ERR#{tag}") { logger.error(line.strip) }
else
output += line
logger.tagged("OUT#{tag}") { logger.info(line.strip) } unless capture
end
rescue EOFError => e
files.delete f
end
end
end
thr.value
end
raise "Execution failed #{code}: #{cmd}" unless code.success?
output
end
|