Class: UnixCommander::Runner
- Inherits:
-
Object
- Object
- UnixCommander::Runner
- Defined in:
- lib/unix_commander.rb
Overview
This class encapsulates the output of a command. It also has the logic to run a command locally or remotely
Instance Attribute Summary collapse
- #command ⇒ Command readonly
Instance Method Summary collapse
-
#both ⇒ Array
Return a string with the output of the command If the command has not been run yet it returns [“”,“”].
-
#err ⇒ String
Returns the output (stderr) of the command If the command has not been run yet it returns an empty string.
-
#initialize(_command) ⇒ Runner
constructor
Creates a new Runner.
-
#out ⇒ String
Returns the output (stdout) of the command If the command has not been run yet it returns an empty string.
-
#run ⇒ Runner
Runs the stored command locally.
-
#run_ssh(_username, _password = "", _address = "127.0.0.1") ⇒ Runner
Runs the stored command remotely.
-
#to_s ⇒ String
Return a string with the output of the command If the command has not been run yet it returns an empty string.
Constructor Details
#initialize(_command) ⇒ Runner
Creates a new Runner
17 18 19 |
# File 'lib/unix_commander.rb', line 17 def initialize(_command) @command = _command end |
Instance Attribute Details
#command ⇒ Command (readonly)
12 13 14 |
# File 'lib/unix_commander.rb', line 12 def command @command end |
Instance Method Details
#both ⇒ Array
Return a string with the output of the command If the command has not been run yet it returns [“”,“”]
47 48 49 |
# File 'lib/unix_commander.rb', line 47 def both [out, err] end |
#err ⇒ String
Returns the output (stderr) of the command If the command has not been run yet it returns an empty string
39 40 41 42 |
# File 'lib/unix_commander.rb', line 39 def err return "" if @err==nil @err end |
#out ⇒ String
Returns the output (stdout) of the command If the command has not been run yet it returns an empty string
31 32 33 34 |
# File 'lib/unix_commander.rb', line 31 def out return "" if @out==nil @out end |
#run ⇒ Runner
Runs the stored command locally
53 54 55 56 57 58 |
# File 'lib/unix_commander.rb', line 53 def run @in, @out, @err = Open3.popen3("#{@command.cmd}") @out = @out.read @err = @err.read self end |
#run_ssh(_username, _password = "", _address = "127.0.0.1") ⇒ Runner
Runs the stored command remotely
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/unix_commander.rb', line 65 def run_ssh(_username, _password = "", _address = "127.0.0.1") stdout_data = "" stderr_data = "" Net::SSH.start(_address,_username,:password => _password) do |ssh| channel = ssh.open_channel do |ch| ch.exec(@command.cmd) do |ch,success| # "on_data" is called when the process writes something to stdout ch.on_data do |c, data| stdout_data += data end # "on_extended_data" is called when the process writes something to stderr ch.on_extended_data do |c, type, data| stderr_data += data end end end end @out = stdout_data @err = stderr_data self end |
#to_s ⇒ String
Return a string with the output of the command If the command has not been run yet it returns an empty string
24 25 26 |
# File 'lib/unix_commander.rb', line 24 def to_s both.join("\n") end |