Class: FlashFlow::CmdRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/flash_flow/cmd_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CmdRunner

Returns a new instance of CmdRunner.



8
9
10
11
12
# File 'lib/flash_flow/cmd_runner.rb', line 8

def initialize(opts={})
  @dir = opts[:dir] || '.'
  @dry_run = opts[:dry_run]
  @logger = opts[:logger] || Logger.new('/dev/null')
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



6
7
8
# File 'lib/flash_flow/cmd_runner.rb', line 6

def dir
  @dir
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



6
7
8
# File 'lib/flash_flow/cmd_runner.rb', line 6

def dry_run
  @dry_run
end

#last_commandObject (readonly)

Returns the value of attribute last_command.



6
7
8
# File 'lib/flash_flow/cmd_runner.rb', line 6

def last_command
  @last_command
end

#last_stderrObject (readonly)

Returns the value of attribute last_stderr.



6
7
8
# File 'lib/flash_flow/cmd_runner.rb', line 6

def last_stderr
  @last_stderr
end

#last_stdoutObject (readonly)

Returns the value of attribute last_stdout.



6
7
8
# File 'lib/flash_flow/cmd_runner.rb', line 6

def last_stdout
  @last_stdout
end

Instance Method Details

#last_success?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/flash_flow/cmd_runner.rb', line 33

def last_success?
  @success
end

#run(cmd) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/flash_flow/cmd_runner.rb', line 14

def run(cmd)
  @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
    @logger.debug("#{dir}$ #{cmd}")
    last_stdout.split("\n").each { |line| @logger.debug(line) }
    last_stderr.split("\n").each { |line| @logger.debug(line) }
  end
end