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

Returns a new instance of Command.



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

def initialize command

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

	if(command.kind_of?(String))
	  self[:input] = command
       end

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

Class Method Details

.exit_code(command) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/command.rb', line 99

def self.exit_code command
	cmd = Command.new(command)
	cmd[:ignore_failure]=true
  cmd[:quiet]=true
	cmd.execute
	cmd[:exit_code]
end

.machineObject



85
86
87
88
89
90
91
92
93
# File 'lib/command.rb', line 85

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

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

.output(command) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/command.rb', line 107

def self.output command
	cmd = Command.new(command)
	cmd[:ignore_failure]=true
  cmd[:quiet]=true
	cmd.execute
	cmd[:output]
end

.userObject



95
96
97
# File 'lib/command.rb', line 95

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

Instance Method Details

#executeObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/command.rb', line 31

def execute
	puts "#{self[:input]}" if(!self.has_key?(:quiet) || self[:quiet])
	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
	    self[:output]=eval(ruby)
	  rescue 
	  	self[:exit_code]=1
	  	self[:error]="unable to eval(#{ruby})"
	  end

	  self[:elapsed] = timer.elapsed_str
	  self[:end_time] = Time.now
	else
		begin
       self[:output],self[:error],status= Open3.capture3(self[:input])
       self[:exit_code]=status.to_i
		  self[:elapsed] = timer.elapsed_str
		  self[:end_time] = Time.now
		rescue Exception => e
		  self[:elapsed] = timer.elapsed_str
		  self[:end_time] = Time.now
		  self[:error] = "Exception: " + e.to_s
		  self[:exit_code]=1
	    end
	end

       Dir.chdir(pwd) if pwd != Dir.pwd

       if(self[:exit_code] != 0)
         if(!self.has_key?(:quiet) || self[:quiet])
       	  puts ' '
       	  puts "exit_code=#{self[:exit_code]}"
       	  puts ' '
       	  puts self[:output]
       	  puts self[:error]
       	  puts ' '
         end
       	if(!self.has_key?(:ignore_failure) || !self[:ignore_failure])
       		raise "#{self[:input]} failed" 
       	end #unless (self.has_key?(:ignore_failure) && self[:ignore_failure]==true)
       end
end

#to_htmlObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/command.rb', line 115

def to_html
  if self[:exit_code] == 0
  [
  	'<div><table><tr><td width="20"></td><td><pre>',
  	self[:input],
  	'</pre></td></tr></table></div>'
  ].join
  else
  [
  	'<div><table><tr><td width="20"></td><td><pre>',
  	self[:input],
    '</pre><table><tr><td width="20"></td><td><table>',
    map { |k, v| ["<tr><td><strong>#{k}</strong></td>", v.respond_to?(:to_html) ? v.to_html : "<td><span><pre>#{v}</pre></span></td></tr>"] },
    '</table>',
    '</td></tr></table></td></tr></table></div>'
  ].join
  end
end