Class: Orator::CLI
- Inherits:
-
Object
- Object
- Orator::CLI
- Defined in:
- lib/orator/cli.rb
Overview
Handles the command line interface for Orator.
Defined Under Namespace
Classes: ProcessExistsError
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :command => :help, :file => "config/orator_config.yml", :daemonize => false }
Class Method Summary collapse
-
.add_orator(klass = nil, &block) ⇒ Array<Class>
Add a orator to be used by the server.
-
.orators ⇒ Array<Class>
A list of orators.
Instance Method Summary collapse
- #handle_command ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #parse_arguments(args) ⇒ Object
- #start ⇒ Object
- #status ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
15 16 17 |
# File 'lib/orator/cli.rb', line 15 def initialize = {}.merge DEFAULT_OPTIONS end |
Class Method Details
.add_orator(klass = nil, &block) ⇒ Array<Class>
Add a orator to be used by the server.
108 109 110 |
# File 'lib/orator/cli.rb', line 108 def self.add_orator(klass = nil, &block) orators << (klass || block) end |
.orators ⇒ Array<Class>
A list of orators.
115 116 117 |
# File 'lib/orator/cli.rb', line 115 def self.orators @orators ||= [] end |
Instance Method Details
#handle_command ⇒ Object
58 59 60 |
# File 'lib/orator/cli.rb', line 58 def handle_command send([:command]) end |
#help ⇒ Object
98 99 100 101 |
# File 'lib/orator/cli.rb', line 98 def help puts @opt_parser exit end |
#parse_arguments(args) ⇒ Object
19 20 21 22 23 24 25 26 27 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 |
# File 'lib/orator/cli.rb', line 19 def parse_arguments(args) @opt_parser = OptionParser.new do |opts| opts.on('--config FILE', "Loads the configuration settings from FILE.") do |file| [:file] = file end opts.on('-cCOMMAND', '--command COMMAND', "The command to run.") do |command| [:command] = command end opts.on('-d', '--[no-]daemonize', "Whether or not to daemonize the process.") do |d| [:daemonize] = d end opts.on('-D', '--debug', "Run Orator in debug mode.") do Orator.debug = true end opts.on('-h', '--help', "Shows this message.") { puts opts; exit } opts.on('-v', '--version', "Shows the version of orator.") do puts Orator::VERSION exit end opts.on('-s', '--slient', "Runs orator silently.") do $stdout = $stdin = File.open("/dev/null", 'w') end opts.separator "" opts.separator "Valid Commands:" opts.separator "\tstart: start the orator server." opts.separator "\tstop: stop the orator server." opts.separator "\tstatus: checks the status of the server. Exits 1 if it's down." end @opt_parser.parse!(args) end |
#start ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/orator/cli.rb', line 62 def start daemonize? do load_orators require_files server = Orator::Server.new([:server_options]) orators = self.class.orators puts "Starting server..." if Orator.debug server.run do orators.each do |orator| if orator.is_a? Proc orator.call(self) else orator.register_with(self) end end end end end |
#status ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/orator/cli.rb', line 90 def status check_pid_file if File.exists? [:pid_file] puts "Not running." exit 1 rescue ProcessExistsError puts "Up, running." end |
#stop ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/orator/cli.rb', line 82 def stop if File.exists? [:pid_file] Process.kill 1, File.open([:pid_file], 'r').read.to_i puts "Stopped Orator." File.unlink [:pid_file] end end |