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
# File 'lib/script_executor/local_command.rb', line 9

def initialize params
  params = sanitize_parameters params

  @password = params[:password]
  @suppress_output = params[:suppress_output]
  @capture_output = params[:capture_output]
  @simulate = params[:simulate]
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

#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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/script_executor/local_command.rb', line 18

def execute commands, line_action
  print_commands commands, $stdout

  unless simulate
    storage = capture_output ? OutputBuffer.new : nil
    output = suppress_output ? nil : $stdout

    commands = commands + inline_password(password) if password

    IO.popen(commands) do |pipe|
      pipe.each("\r") do |line|
        output.print(line) if output
        storage.save(line.chomp) if storage

        line_action.call(line) if line_action
      end
    end

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