Module: RunCommand
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #command_line ⇒ Object
- #command_line_opts ⇒ Object
- #command_output ⇒ Object
- #data ⇒ Object
- #options ⇒ Object
- #set_opts(opts = {}) ⇒ Object
- #supported? ⇒ Boolean
Class Method Details
.included(base) ⇒ Object
81 82 83 84 |
# File 'lib/city_watch/util/run_command.rb', line 81 def self.included(base) base.extend(ClassMethods) Commands.register(base) end |
Instance Method Details
#command_line ⇒ Object
17 18 19 |
# File 'lib/city_watch/util/run_command.rb', line 17 def command_line @command ||= "#{[:command]} #{command_line_opts}#{[:grep] && " | fgrep #{[:grep]}"}" end |
#command_line_opts ⇒ Object
21 22 23 24 25 26 |
# File 'lib/city_watch/util/run_command.rb', line 21 def command_line_opts .inject([]) do |acc,(k,v)| acc << "-#{k} #{v}" unless [:command, :grep].include?(k) acc end.join(" ") end |
#command_output ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/city_watch/util/run_command.rb', line 3 def command_output unless !supported? puts "Running `#{command_line}`..." if CityWatch.debug? `#{command_line}` else puts "Command not present: #{[:command]} (Skipping)" if CityWatch.debug? "" end end |
#data ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/city_watch/util/run_command.rb', line 37 def data headers = false output = [] command_output.split("\n").map {|line| v = line.split("\s"); v.shift; v }.each do |line| if !headers headers = line.map {|hdr| hdr.downcase.to_sym} next end next unless headers pkt = {} line.each_with_index do |itm,idx| pkt[headers[idx]] = itm end output << pkt end output end |
#options ⇒ Object
28 29 30 |
# File 'lib/city_watch/util/run_command.rb', line 28 def @opts ||= self.class. end |
#set_opts(opts = {}) ⇒ Object
32 33 34 35 |
# File 'lib/city_watch/util/run_command.rb', line 32 def set_opts(opts={}) @opts = self.class..merge(opts) @command = nil end |
#supported? ⇒ Boolean
13 14 15 |
# File 'lib/city_watch/util/run_command.rb', line 13 def supported? `which #{[:command]}` != "" end |