lib/command.rb in dev_commands-0.0.1 vs lib/command.rb in dev_commands-0.0.2
- old
+ new
@@ -1,57 +1,116 @@
+require_relative('./timer.rb')
+
class Command < Hash
+ 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
+
+ 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
+
end
\ No newline at end of file