Class: Command

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

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Command

Returns a new instance of Command.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/command.rb', line 2

def initialize command

  if(command.kind_of?(String))
    self[:input] = command
    self[:timeout] = 0
    self[:directory] = ''
    self[:exit_code] = 0
    self[:output] = ''
    self[:error] = ''
    self[:machine_name] = ''
    self[:user_name] = ''
    self[:start_time] = nil
    self[:end_time] = nil
       end

       if(command.kind_of?(Hash))
        command.each{|k,v|
          self[k.to_sym]=v
        }
       end
end

Instance Method Details

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/command.rb', line 24

def execute

  Logger.start_command self

  pwd=Dir.pwd
  Dir.chdir(self[:directory]) if(self.has_key?(:directory) && File.exists?(self[:directory]))
  #print " " + Color.green + self[:input] + Color.clear


  self[:start_time]=Time.now
  timer=Timer.new
  if self[:input].include?('<%') && self[:input].include?('%>')
    ruby = self[:input].gsub("<%","").gsub("%>","")

    begin
     puts "eval(#{ruby})"
      self[:output]=eval(ruby)
    rescue
     puts "unable to eval(#{ruby})"
     raise "unable to eval(#{ruby})"
    end

    #puts " " + timer.elapsed_str

    self[:elapsed] = timer.elapsed_str
    self[:end_time] = Time.now
  else
    self[:output] = `#{self[:input]}`
    self[:elapsed] = timer.elapsed_str
    self[:end_time] = Time.now
    self[:exit_code]=$?.to_i
  end

       Dir.chdir(pwd) if pwd != Dir.pwd
  Logger.end_command self
end