Class: UnixCommander::Runner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(_command) ⇒ Runner

Creates a new Runner

Parameters:

  • _command

    The command to be run by the runner



17
18
19
# File 'lib/unix_commander.rb', line 17

def initialize(_command)
  @command = _command
end

Instance Attribute Details

#commandCommand (readonly)

Returns:



12
13
14
# File 'lib/unix_commander.rb', line 12

def command
  @command
end

Instance Method Details

#bothArray

Return a string with the output of the command If the command has not been run yet it returns [“”,“”]

Returns:

  • (Array)

    stdout and stderr of the command



47
48
49
# File 'lib/unix_commander.rb', line 47

def both
  [out, err]
end

#errString

Returns the output (stderr) of the command If the command has not been run yet it returns an empty string

Returns:

  • (String)

    stderr of the command



39
40
41
42
# File 'lib/unix_commander.rb', line 39

def err
  return "" if @err==nil
  @err
end

#outString

Returns the output (stdout) of the command If the command has not been run yet it returns an empty string

Returns:

  • (String)

    stdout of the command



31
32
33
34
# File 'lib/unix_commander.rb', line 31

def out
  return "" if @out==nil
  @out
end

#runRunner

Runs the stored command locally

Returns:



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

Parameters:

  • _username

    The ssh username to access the remote server

  • _password (defaults to: "")

    The ssh password to access the remote server

  • _address (defaults to: "127.0.0.1")

    The ssh server address to access the remote server. It defaults to localhost.

Returns:



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_sString

Return a string with the output of the command If the command has not been run yet it returns an empty string

Returns:

  • (String)

    stdout and stderr of the command (stdout \n stderr)



24
25
26
# File 'lib/unix_commander.rb', line 24

def to_s
  both.join("\n")
end