Class: UnixCommander::Command
- Inherits:
-
Object
- Object
- UnixCommander::Command
- 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
- #cmd ⇒ String readonly
Instance Method Summary collapse
-
#both_to(_str, _append = false) ⇒ Command
Redirects stdout and stderr to someplace (Using >).
-
#err_to(_str, _append = false) ⇒ Command
Redirects stderr to someplace (Using >).
-
#initialize(_cmd = "") ⇒ Command
constructor
Creates a new command.
-
#method_missing(m, *args, &block) ⇒ Command
This is the main method of the library.
-
#out_to(_str, _append = false) ⇒ Command
Redirects stdout to someplace (Using >).
-
#run ⇒ Runner
Run the Command locally.
-
#run_ssh(_username, _password = "", _address = "127.0.0.1") ⇒ Runner
Run the Command remotely via ssh.
-
#to_s ⇒ String
Shows the string representation of the command being run in unix.
Constructor Details
#initialize(_cmd = "") ⇒ Command
Creates a new command
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 (|)
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
#cmd ⇒ String (readonly)
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.
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.
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.
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 |
#run ⇒ Runner
Run the Command locally. The output is encapsulated in a Runner object
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
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_s ⇒ String
Shows the string representation of the command being run in unix
102 103 104 |
# File 'lib/unix_commander.rb', line 102 def to_s cmd end |