Class: GenericCommand
- Inherits:
-
Object
- Object
- GenericCommand
- Defined in:
- lib/CommandManager.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.run(command, logger = nil, stdin = nil) ⇒ Object
Creates a command and runs it.
Instance Method Summary collapse
-
#get_error_message ⇒ Object
Parses error message from
stderr
output. -
#initialize(command, logger = nil, stdin = nil) ⇒ GenericCommand
constructor
Creates the new command:
command
: string with the command to be executedlogger
: proc that takes a message parameter and logs it. -
#log(message) ⇒ Object
Sends a log message to the logger proc.
-
#run ⇒ Object
Runs the command.
Constructor Details
#initialize(command, logger = nil, stdin = nil) ⇒ GenericCommand
Creates the new command: command
: string with the command to be executed logger
: proc that takes a message parameter and logs it
57 58 59 60 61 |
# File 'lib/CommandManager.rb', line 57 def initialize(command, logger=nil, stdin=nil) @command = command @logger = logger @stdin = stdin end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
45 46 47 |
# File 'lib/CommandManager.rb', line 45 def code @code end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
45 46 47 |
# File 'lib/CommandManager.rb', line 45 def command @command end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
45 46 47 |
# File 'lib/CommandManager.rb', line 45 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
45 46 47 |
# File 'lib/CommandManager.rb', line 45 def stdout @stdout end |
Class Method Details
.run(command, logger = nil, stdin = nil) ⇒ Object
Creates a command and runs it
48 49 50 51 52 |
# File 'lib/CommandManager.rb', line 48 def self.run(command, logger=nil, stdin=nil) cmd = self.new(command, logger, stdin) cmd.run cmd end |
Instance Method Details
#get_error_message ⇒ Object
Parses error message from stderr
output
96 97 98 99 100 |
# File 'lib/CommandManager.rb', line 96 def tmp=@stderr.scan(/^ERROR MESSAGE --8<------\n(.*?)ERROR MESSAGE ------>8--$/m) return "-" if !tmp[0] tmp[0].join(' ').strip end |
#log(message) ⇒ Object
Sends a log message to the logger proc
64 65 66 |
# File 'lib/CommandManager.rb', line 64 def log() @logger.call() if @logger end |
#run ⇒ Object
Runs the command
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/CommandManager.rb', line 69 def run std = execute # Close standard IO descriptors if @stdin std[0] << @stdin std[0].flush end std[0].close if !std[0].closed? @stdout=std[1].read std[1].close if !std[1].closed? @stderr=std[2].read std[2].close if !std[2].closed? @code=get_exit_code(@stderr) if @code!=0 log("Command execution fail: #{command}") log(@stderr) end return @code end |