Class: CommandExec::Command
- Inherits:
-
Object
- Object
- CommandExec::Command
- Defined in:
- lib/command_exec/command.rb
Overview
Run commands
Instance Attribute Summary collapse
-
#error_keywords ⇒ Object
Returns the value of attribute error_keywords.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parameter ⇒ Object
Returns the value of attribute parameter.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#working_directory ⇒ Object
readonly
Returns the value of attribute working_directory.
Class Method Summary collapse
-
.execute(name, opts = {}) ⇒ Object
Constructur to initiate a new command and run it later.
Instance Method Summary collapse
-
#error_in_string_found?(keywords = [], string) ⇒ Boolean
Find error in stdout.
-
#help_output(error_indicators = {}, output = {}) ⇒ Array
Decide which output to return to the user to help him with debugging.
-
#initialize(name, opts = {}) ⇒ Command
constructor
Create a new command to execute.
-
#message(run_successful, *msg) ⇒ Object
Generate the message which is return to the user.
-
#read_logfile(file, num_of_lines = -30)) ⇒ Object
Read the content of the logfile.
-
#run ⇒ Object
Run the program.
-
#run_successful?(success, error_in_stdout) ⇒ Boolean
Decide if a program run was successful.
-
#to_txt ⇒ String
Output the textual representation of a command public alias for build_cmd_string.
Constructor Details
#initialize(name, opts = {}) ⇒ Command
Create a new command to execute
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/command_exec/command.rb', line 20 def initialize(name,opts={}) @name = name @opts = { :logger => Logger.new($stderr), :options => '', :parameter => '', :error_keywords => [], :working_directory => Dir.pwd, :logfile => '', :log_level => :info, }.update opts @logger = @opts[:logger] @options = @opts[:options] @parameter = @opts[:parameter] @path = resolve_cmd_name(name) @error_keywords = @opts[:error_keywords] @logfile = @opts[:logfile] configure_logging @working_directory = @opts[:working_directory] Dir.chdir(working_directory) end |
Instance Attribute Details
#error_keywords ⇒ Object
Returns the value of attribute error_keywords.
8 9 10 |
# File 'lib/command_exec/command.rb', line 8 def error_keywords @error_keywords end |
#logfile ⇒ Object
Returns the value of attribute logfile.
8 9 10 |
# File 'lib/command_exec/command.rb', line 8 def logfile @logfile end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/command_exec/command.rb', line 8 def @options end |
#parameter ⇒ Object
Returns the value of attribute parameter.
8 9 10 |
# File 'lib/command_exec/command.rb', line 8 def parameter @parameter end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/command_exec/command.rb', line 9 def path @path end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
9 10 11 |
# File 'lib/command_exec/command.rb', line 9 def result @result end |
#working_directory ⇒ Object (readonly)
Returns the value of attribute working_directory.
9 10 11 |
# File 'lib/command_exec/command.rb', line 9 def working_directory @working_directory end |
Class Method Details
.execute(name, opts = {}) ⇒ Object
Constructur to initiate a new command and run it later
243 244 245 246 247 248 |
# File 'lib/command_exec/command.rb', line 243 def Command.execute(name,opts={}) command = new(name,opts) command.run command end |
Instance Method Details
#error_in_string_found?(keywords = [], string) ⇒ Boolean
Find error in stdout
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/command_exec/command.rb', line 208 def error_in_string_found? (keywords=[], string ) return false if keywords.empty? or not keywords.is_a? Array return false if string.nil? or not string.is_a? String error_found = false keywords.each do |word| if string.include? word error_found = true break end end error_found end |
#help_output(error_indicators = {}, output = {}) ⇒ Array
Decide which output to return to the user to help him with debugging
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/command_exec/command.rb', line 180 def help_output(error_indicators={},output={}) error_in_exec = error_indicators[:error_in_exec] error_in_stdout = error_indicators[:error_in_stdout] logfile = output[:logfile].string stdout = output[:stdout].string stderr = output[:stderr].string result = [] if error_in_exec == true result << '================== LOGFILE ================== ' result << logfile if logfile.empty? == false result << '================== STDOUT ================== ' result << stdout if stdout.empty? == false result << '================== STDERR ================== ' result << stderr if stderr.empty? == false elsif error_in_stdout == true result << '================== STDOUT ================== ' result << stdout end result end |
#message(run_successful, *msg) ⇒ Object
Generate the message which is return to the user
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/command_exec/command.rb', line 227 def (run_successful, *msg) = [] if run_successful << 'OK'.green.bold else << 'FAILED'.red.bold .concat msg.flatten end .join("\n") end |
#read_logfile(file, num_of_lines = -30)) ⇒ Object
Read the content of the logfile
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/command_exec/command.rb', line 151 def read_logfile(file, num_of_lines=-30) content = StringIO.new unless file.empty? begin content << File.readlines(logfile)[num_of_lines..-1].join("") rescue Errno::ENOENT @logger.warn "Warning: logfile not found!" end end content end |
#run ⇒ Object
Run the program
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/command_exec/command.rb', line 110 def run _stdout = '' _stderr = '' status = POpen4::popen4(build_cmd_string) do |stdout, stderr, stdin, pid| _stdout = stdout.read.strip _stderr = stderr.read.strip end error_in_stdout_found = error_in_string_found?(error_keywords,_stdout) @result = run_successful?( status.success? , error_in_stdout_found ) if @result == false msg = ( @result, help_output( { :error_in_exec => not(status.success?), :error_in_stdout => error_in_stdout_found }, { :stdout => StringIO.new(_stdout), :stderr => StringIO.new(_stderr), :logfile => read_logfile(logfile), } ) ) else msg = (@result) end @logger.info "#{@name.to_s}: #{msg}" @result end |
#run_successful?(success, error_in_stdout) ⇒ Boolean
Decide if a program run was successful
168 169 170 171 172 173 174 |
# File 'lib/command_exec/command.rb', line 168 def run_successful?(success,error_in_stdout) if success == false or error_in_stdout == true return false else return true end end |
#to_txt ⇒ String
Output the textual representation of a command public alias for build_cmd_string
104 105 106 |
# File 'lib/command_exec/command.rb', line 104 def to_txt build_cmd_string end |