Class: Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Command



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/command.rb', line 6

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] = ''
	  self[:user] = ''
	  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

Class Method Details

.machineObject



61
62
63
64
65
66
67
68
69
# File 'lib/command.rb', line 61

def self.machine
  if !ENV['COMPUTERNAME'].nil? 
return ENV['COMPUTERNAME']
	  end

  machine = `hostname`
  machine = machine.split('.')[0] if machine.include?('.')
	  return machine.strip
end

.userObject



71
72
73
# File 'lib/command.rb', line 71

def self.user
  ENV['USER'].nil? ? ENV['USERNAME'] : ENV['USER']
end

Instance Method Details

#executeObject



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
58
59
# File 'lib/command.rb', line 28

def execute
	pwd=Dir.pwd
	Dir.chdir(self[:directory]) if(self.has_key?(:directory) && File.exists?(self[:directory]))
	self[:directory] = pwd if(self[:directory].length==0)
	
	self[:machine] = Command.machine
	self[:user] = Command.user

	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

	  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
end

#to_htmlObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/command.rb', line 75

def to_html
  [
  	'<h2>',
  	self[:input],
  	'</h2>',
    '<ul>',
    map { |k, v| ["<li><strong>#{k}</strong>", v.respond_to?(:to_html) ? v.to_html : "<span>#{v}</span></li>"] },
    '</ul>'
  ].join
end