class FlashFlow::CmdRunner

Constants

LOG_CMD
LOG_NONE

Attributes

dir[RW]
dry_run[R]
last_command[R]
last_stderr[R]
last_stdout[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/flash_flow/cmd_runner.rb, line 12
def initialize(opts={})
  @dir = opts[:dir] || `pwd`.strip
  @dry_run = opts[:dry_run]
  @logger = opts[:logger] || Logger.new('/dev/null')
end

Public Instance Methods

last_success?() click to toggle source
# File lib/flash_flow/cmd_runner.rb, line 35
def last_success?
  @success
end
run(cmd, opts={}) click to toggle source
# File lib/flash_flow/cmd_runner.rb, line 18
def run(cmd, opts={})
  @last_command = cmd
  if dry_run
    puts "#{dir}$ #{cmd}"
    ''
  else
    Dir.chdir(dir) do
      Open3.popen3(cmd) do |_, stdout, stderr, wait_thr|
        @last_stdout = stdout.read
        @last_stderr = stderr.read
        @success = wait_thr.value.success?
      end
    end
    log(cmd, opts[:log])
  end
end

Private Instance Methods

log(cmd, log_what) click to toggle source
# File lib/flash_flow/cmd_runner.rb, line 41
def log(cmd, log_what)
  log_what = nil
  if log_what == LOG_NONE
      # Do nothing
  else
      @logger.debug("#{dir}$ #{cmd}")
    unless log_what == LOG_CMD
      last_stdout.split("\n").each { |line| @logger.debug(line) }
      last_stderr.split("\n").each { |line| @logger.debug(line) }
    end
  end
end