Class: LocalCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/script_executor/local_command.rb

Overview

require “open3” @stdout, @stderr, @status = Open3.capture3(stdin)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ LocalCommand

Returns a new instance of LocalCommand.



9
10
11
12
13
14
15
16
17
# File 'lib/script_executor/local_command.rb', line 9

def initialize(params)
  params = sanitize_parameters params

  @password = params[:password]
  @simulate = params[:simulate]
  @suppress_output = params[:suppress_output]
  @capture_output = params[:capture_output]
  @output_stream = params[:output_stream]
end

Instance Attribute Details

#capture_outputObject (readonly)

Returns the value of attribute capture_output.



7
8
9
# File 'lib/script_executor/local_command.rb', line 7

def capture_output
  @capture_output
end

#output_streamObject (readonly)

Returns the value of attribute output_stream.



7
8
9
# File 'lib/script_executor/local_command.rb', line 7

def output_stream
  @output_stream
end

#passwordObject (readonly)

Returns the value of attribute password.



7
8
9
# File 'lib/script_executor/local_command.rb', line 7

def password
  @password
end

#simulateObject

Returns the value of attribute simulate.



5
6
7
# File 'lib/script_executor/local_command.rb', line 5

def simulate
  @simulate
end

#suppress_outputObject (readonly)

Returns the value of attribute suppress_output.



7
8
9
# File 'lib/script_executor/local_command.rb', line 7

def suppress_output
  @suppress_output
end

Instance Method Details

#execute(commands, line_action) ⇒ Object



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
45
46
47
48
49
# File 'lib/script_executor/local_command.rb', line 19

def execute(commands, line_action)
  print_commands commands, $stdout

  unless simulate
    storage = capture_output ? OutputBuffer.new : nil
    output = if output_stream
               output_stream
             else
               suppress_output ? nil : $stdout
             end

    commands = commands + inline_password(password) if password

    IO.popen(commands) do |io|
      io.each_line("\r") do |line|
        if output
          output.write(line)
          output.flush
        end

        if storage
          storage.save(line.chomp)
        end

        line_action.call(line) if line_action
      end
    end

    storage.buffer.join("\n") if storage
  end
end