Class: Flintlock::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/flintlock/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



5
6
7
# File 'lib/flintlock/runner.rb', line 5

def initialize(options={})
  @log = Util.load_logger(!!options[:debug])
end

Instance Method Details

#run(command, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flintlock/runner.rb', line 9

def run(command, options={})
  @log.debug("running command: '#{command.inspect}'")

  options[:capture] = !!options[:capture]
  options[:env] = options.fetch(:env, ENV)

  rout, wout = IO.pipe
  rerr, werr = IO.pipe
  pid = Process.spawn(options[:env], Shellwords.join(command), :out => wout, :err => werr)

  Process.wait(pid)
  status = $?.exitstatus

  # capture stdout/stderr
  wout.close
  werr.close
  stdout = rout.read
  stderr = rerr.read

  @log.linewise(stdout, :level => :info)
  @log.linewise(stderr)

  return options[:capture] ? [stdout, stderr, status] : status 
end