Class: UnixCommander::Command

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

Overview

This class encapsulates a command that will run in a unix machine locally or remotely

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_cmd = "") ⇒ Command

Creates a new command

Parameters:

  • _cmd (String) (defaults to: "")

    create a command with some unix code inside (defaults to “”)



97
98
99
# File 'lib/unix_commander.rb', line 97

def initialize(_cmd = "")
  @cmd = _cmd
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Command

This is the main method of the library. Every unknown method you call on a Command object is interpreted as a unix command and its args are used as the args of the unix command. When the command already has some unix command inside, it pipes them together (|)

Parameters:

  • m (String)

    name of the unix command you want to execute

  • args (Array)

    args for the aforementioned command

Returns:

  • (Command)

    new command with internal unix commands piped together



163
164
165
166
167
168
169
# File 'lib/unix_commander.rb', line 163

def method_missing(m, *args, &block)
  if cmd == ""
    Command.new("#{m} #{args.join(' ')}".strip)
  else
    Command.new("#{cmd} | #{m} #{args.join(' ')}".strip)
  end
end

Instance Attribute Details

#cmdString (readonly)

Returns:

  • (String)


93
94
95
# File 'lib/unix_commander.rb', line 93

def cmd
  @cmd
end

Instance Method Details

#both_to(_str, _append = false) ⇒ Command

Redirects stdout and stderr to someplace (Using >). By default it uses destructive redirection.

Parameters:

  • _str (String)

    place to redirect the output (e.g. /dev/null)

  • _append (true, false) (defaults to: false)

    if true uses append redirection (>>) it defaults to false.

Returns:

  • (Command)

    New command with *stdout and stderr* redirected to _str



134
135
136
137
138
139
140
# File 'lib/unix_commander.rb', line 134

def both_to(_str,_append=false)
  if cmd == ""
    raise ArgumentError, "Cannot redirect with an empty command"
  else
    _append ? Command.new("#{cmd} >> #{_str} 2>&1") : Command.new("#{cmd} > #{_str} 2>&1")
  end
end

#err_to(_str, _append = false) ⇒ Command

Redirects stderr to someplace (Using >). By default it uses destructive redirection.

Parameters:

  • _str (String)

    place to redirect the output (e.g. /dev/null)

  • _append (true, false) (defaults to: false)

    if true uses append redirection (>>) it defaults to false.

Returns:

  • (Command)

    New command with stderr redirected to _str



122
123
124
125
126
127
128
# File 'lib/unix_commander.rb', line 122

def err_to(_str,_append=false)
  if cmd == ""
    raise ArgumentError, "Cannot redirect with an empty command"
  else
    _append ? Command.new("#{cmd} 2>> #{_str}") : Command.new("#{cmd} 2> #{_str}")
  end
end

#out_to(_str, _append = false) ⇒ Command

Redirects stdout to someplace (Using >). By default it uses destructive redirection.

Parameters:

  • _str (String)

    place to redirect the output (e.g. /dev/null)

  • _append (true, false) (defaults to: false)

    if true uses append redirection (>>) it defaults to false.

Returns:

  • (Command)

    New command with stdout redirected to _str



110
111
112
113
114
115
116
# File 'lib/unix_commander.rb', line 110

def out_to(_str,_append=false)
  if cmd == ""
    raise ArgumentError, "Cannot redirect with an empty command"
  else
    _append ? Command.new("#{cmd} >> #{_str}") : Command.new("#{cmd} > #{_str}")
  end
end

#runRunner

Run the Command locally. The output is encapsulated in a Runner object

Returns:

  • (Runner)

    runner object with the output inside



144
145
146
# File 'lib/unix_commander.rb', line 144

def run
  Runner.new(self).run
end

#run_ssh(_username, _password = "", _address = "127.0.0.1") ⇒ Runner

Run the Command remotely via ssh. The output is encapsulated in a Runner object

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:

  • (Runner)

    runner object with the output inside



153
154
155
# File 'lib/unix_commander.rb', line 153

def run_ssh(_username, _password = "", _address = "127.0.0.1")
  Runner.new(self).run_ssh(_username,_password,_address)
end

#to_sString

Shows the string representation of the command being run in unix

Returns:

  • (String)


102
103
104
# File 'lib/unix_commander.rb', line 102

def to_s
  cmd
end