Class: CMD

Inherits:
Hash
  • Object
show all
Defined in:
lib/cmd.rb,
lib/cmd_windows.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, options = nil) ⇒ CMD

Returns a new instance of CMD.



9
10
11
12
13
14
15
16
17
# File 'lib/cmd.rb', line 9

def initialize(cmd, options=nil)
 initialize_defaults if(@@default_options.nil?)
 self[:output] = ''
 self[:error] = ''
 
 @@default_options.each { |key, value| self[key] = value}
 options.each { |key, value| self[key] = value} unless(options.nil?)
 self[:command]=cmd
end

Class Method Details

.default_options(hash) ⇒ Object



20
21
22
# File 'lib/cmd.rb', line 20

def self.default_options(hash)
  hash.each { |key, value| @@default_options[key] = value}
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
# File 'lib/cmd.rb', line 24

def execute
  if(self[:quiet])
 self[:echo_output] = false
 self[:echo_command] = false
  end
  
  puts self[:command] if(self[:echo_command] || self[:debug])
  system  
    
  if(self[:debug])
 puts "command: #{self[:command]}"
 puts "output: #{self[:output]}"
 puts "error: #{self[:error]}"
 puts "exit_code: #{self[:exit_code]}"
  end
  
  if((self[:exit_code] != 0) && !self[:ignore_exit_code])
 exception_text = "Exit code: #{self[:exit_code]}"
 exception_text = "#{exception_text}\nError: '#{self[:error]}'"
 exception_text = "#{exception_text}\nOutput: '#{self[:output]}'"
 raise StandardError.new(exception_text) 
  end
end

#execute_as(username) ⇒ Object



6
7
8
9
# File 'lib/cmd_windows.rb', line 6

def execute_as(username)
  self[:command] = "runas /noprofile /savecred /user:#{username} \"#{self[:command]}\""
  wait_on_spawned_process(self) { self.execute }
end

#systemObject



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
84
85
86
87
88
89
# File 'lib/cmd.rb', line 48

def system
  begin
    output = ''
 error = ''  
 Thread.abort_on_exception = true
 mutex = Mutex.new
 
 Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
      self[:pid] = wait_thr.pid 
      {:output => stdout,:error => stderr}.each do |key, stream|
        Thread.new do         
    while wait_thr.alive? do
      if(!(char = stream.getc).nil?)
      case key
        when :output
          output << char
        when :error
          error << char
      end

      mutex.synchronize { putc char if(self[:echo_output]) }
      else
        sleep(0.1)
    end
       end
  end
end

wait_thr.join

   self[:output] = output unless(output.empty?)
   self[:error] = error unless(error.empty?)
self[:exit_code] = wait_thr.value.to_i
 end
  rescue Exception => e
 self[:error] = "#{self[:error]}\nException: #{e.to_s}"
 self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
  rescue => e
 self[:error] = "#{self[:error]}\nException: #{e.to_s}"
 self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
  end
end