Class: CommandExec::Command

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

Overview

Run commands

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Command

Create a new command to execute

Parameters:

  • name (Symbol)

    name of command

  • opts (optional, Hash) (defaults to: {})

    options for the command

Options Hash (opts):

  • :options (String)

    options for binary

  • :parameter (String)

    parameter for binary

  • :error_keywords (String)

    keyword indicating an error on stdout

  • :working_directory (String)

    working directory where the process should run in

  • :logfile (String)

    file path to log file of process



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_keywordsObject

Returns the value of attribute error_keywords.



8
9
10
# File 'lib/command_exec/command.rb', line 8

def error_keywords
  @error_keywords
end

#logfileObject

Returns the value of attribute logfile.



8
9
10
# File 'lib/command_exec/command.rb', line 8

def logfile
  @logfile
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/command_exec/command.rb', line 8

def options
  @options
end

#parameterObject

Returns the value of attribute parameter.



8
9
10
# File 'lib/command_exec/command.rb', line 8

def parameter
  @parameter
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/command_exec/command.rb', line 9

def path
  @path
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/command_exec/command.rb', line 9

def result
  @result
end

#working_directoryObject (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

See Also:



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

Returns:

  • (Boolean)

    Returns true if it finds an error



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

Returns:

  • (Array)

    Returns lines of log/stdout/stderr



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

Parameters:

  • run_successful (Boolean)

    true if a positive message should be returned

  • msg (Array)

    Message which should be returned



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/command_exec/command.rb', line 227

def message(run_successful, *msg)

  message = []
  if run_successful
    message << 'OK'.green.bold
  else
    message << 'FAILED'.red.bold
    message.concat msg.flatten
  end

  message.join("\n")
end

#read_logfile(file, num_of_lines = -30)) ⇒ Object

Read the content of the logfile

Parameters:

  • file (Path)

    path to logfile

  • num_of_lines (Integer) (defaults to: -30))

    the number of lines which should be read – e.g. 30 lines = -30



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

#runObject

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 = message(
      @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 =  message(@result)
  end

  @logger.info "#{@name.to_s}: #{msg}"

  @result
end

#run_successful?(success, error_in_stdout) ⇒ Boolean

Decide if a program run was successful

Returns:

  • (Boolean)

    Returns the decision



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_txtString

Output the textual representation of a command public alias for build_cmd_string

Returns:

  • (String)

    command in text form



104
105
106
# File 'lib/command_exec/command.rb', line 104

def to_txt 
  build_cmd_string
end