Class: Rubicante::CLI
- Inherits:
-
Object
- Object
- Rubicante::CLI
- Defined in:
- lib/rubicante/cli.rb
Overview
A command-line interface for Rubicante
Instance Method Summary collapse
-
#handle(cmd) ⇒ Object
Handles sending the command to the Environment and dealing with any return values that require output to the user.
-
#initialize(debug = false) ⇒ CLI
constructor
A new instance of CLI.
-
#load(file) ⇒ Object
Load in a file, executing each line.
- #process(resp) ⇒ Object
- #process_host_error(resp) ⇒ Object
- #process_port_errors(resp) ⇒ Object
- #process_service_errors(resp) ⇒ Object
- #process_website_errors(resp) ⇒ Object
- #puts_error(hostname, error) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(debug = false) ⇒ CLI
Returns a new instance of CLI.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rubicante/cli.rb', line 8 def initialize(debug = false) # Set up the logger for this CLI session @log = Logging.logger['rubicante'] @log.add_appenders(Logging.appenders.stdout) @log.level = debug ? :debug : :info # Set up logger for Environment Logging.logger['Rubicante::Environment'].add_appenders(Logging.appenders.stdout) Logging.logger['Rubicante::Environment'].level = @log.level # Prepare Environment @env = Environment.new end |
Instance Method Details
#handle(cmd) ⇒ Object
Handles sending the command to the Environment and dealing with any return values that require output to the user
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubicante/cli.rb', line 68 def handle(cmd) exit if cmd == "exit" begin result = @env.eval_command(cmd) rescue NotImplementedError @log.error "Unrecognized command '#{cmd.split[0]}'." end # 'what' commands return Arrays if result.kind_of? Array result.each do |response| process(response) end end end |
#load(file) ⇒ Object
Load in a file, executing each line
61 62 63 64 |
# File 'lib/rubicante/cli.rb', line 61 def load(file) cmds = File.new(file) cmds.each_line { |cmd| handle(cmd) } end |
#process(resp) ⇒ Object
22 23 24 25 26 |
# File 'lib/rubicante/cli.rb', line 22 def process(resp) if resp.kind_of? HostError process_host_error(resp) end end |
#process_host_error(resp) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/rubicante/cli.rb', line 28 def process_host_error(resp) if not resp.ping puts_error(resp.hostname, "is unreachable.") else process_port_errors(resp) process_service_errors(resp) process_website_errors(resp) end end |
#process_port_errors(resp) ⇒ Object
38 39 40 41 42 |
# File 'lib/rubicante/cli.rb', line 38 def process_port_errors(resp) resp.bad_ports.each do |bad_port| puts_error(resp.hostname, "port #{bad_port} is closed.") end end |
#process_service_errors(resp) ⇒ Object
44 45 46 47 48 |
# File 'lib/rubicante/cli.rb', line 44 def process_service_errors(resp) resp.bad_services.each do |bad_service| puts_error(resp.hostname, "service #{bad_service} is stopped.") end end |
#process_website_errors(resp) ⇒ Object
50 51 52 53 54 |
# File 'lib/rubicante/cli.rb', line 50 def process_website_errors(resp) resp.website_errors.each do |website_error| puts_error(resp.hostname, "is returning code #{website_error.code} for website #{website_error.url}.") end end |
#puts_error(hostname, error) ⇒ Object
56 57 58 |
# File 'lib/rubicante/cli.rb', line 56 def puts_error(hostname, error) @log.info "[#{hostname}] #{error}" end |
#run ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/rubicante/cli.rb', line 85 def run while true do print 'rubicante> ' cmd = gets.downcase.strip handle(cmd) end end |