Class: FlashFlow::CmdRunner

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

Constant Summary collapse

LOG_NONE =
:log_none
LOG_CMD =
:log_cmd

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CmdRunner

Returns a new instance of CmdRunner.



12
13
14
15
16
# 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

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



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

def dir
  @dir
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



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

def dry_run
  @dry_run
end

#last_commandObject (readonly)

Returns the value of attribute last_command.



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

def last_command
  @last_command
end

#last_stderrObject (readonly)

Returns the value of attribute last_stderr.



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

def last_stderr
  @last_stderr
end

#last_stdoutObject (readonly)

Returns the value of attribute last_stdout.



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

def last_stdout
  @last_stdout
end

Instance Method Details

#last_success?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/flash_flow/cmd_runner.rb', line 35

def last_success?
  @success
end

#run(cmd, opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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