Class: Orator::CLI

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



15
16
17
# File 'lib/orator/cli.rb', line 15

def initialize
  @options = {}.merge DEFAULT_OPTIONS
end

Class Method Details

.add_orator(klass = nil, &block) ⇒ Array<Class>

Add a orator to be used by the server.

Parameters:

  • klass (Class) (defaults to: nil)

    the class to use as aorator.

Returns:

  • (Array<Class>)

    a list of orators that will be used for the server.



108
109
110
# File 'lib/orator/cli.rb', line 108

def self.add_orator(klass = nil, &block)
  orators << (klass || block)
end

.oratorsArray<Class>

A list of orators.

Returns:

  • (Array<Class>)

    the orators.



115
116
117
# File 'lib/orator/cli.rb', line 115

def self.orators
  @orators ||= []
end

Instance Method Details

#handle_commandObject



58
59
60
# File 'lib/orator/cli.rb', line 58

def handle_command
  send(@options[:command])
end

#helpObject



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|
      @options[:file] = file
    end

    opts.on('-cCOMMAND', '--command COMMAND', "The command to run.") do |command|
      @options[:command] = command
    end

    opts.on('-d', '--[no-]daemonize', "Whether or not to daemonize the process.") do |d|
      @options[: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

#startObject



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(yaml_options[: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

#statusObject



90
91
92
93
94
95
96
# File 'lib/orator/cli.rb', line 90

def status
  check_pid_file if File.exists? yaml_options[:pid_file]
  puts "Not running."
  exit 1
rescue ProcessExistsError
  puts "Up, running."
end

#stopObject



82
83
84
85
86
87
88
# File 'lib/orator/cli.rb', line 82

def stop
  if File.exists? yaml_options[:pid_file]
    Process.kill 1, File.open(yaml_options[:pid_file], 'r').read.to_i
    puts "Stopped Orator."
    File.unlink yaml_options[:pid_file]
  end
end