Class: RabbitWQ::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbit_wq/cli.rb

Constant Summary collapse

SUB_COMMANDS =
%w(
  restart
  start
  status
  stop
)
DEFAULT_CONFIG_PATH =
"/etc/#{APP_ID}/#{APP_ID}.conf"
DEFAULT_LOG_PATH =
"/var/log/#{APP_ID}/#{APP_ID}.log"
DEFAULT_PID_PATH =
"/var/run/#{APP_ID}/#{APP_ID}.pid"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



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
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
# File 'lib/rabbit_wq/cli.rb', line 23

def initialize( args )
  Trollop::options do
    version VERSION_COPYRIGHT
    banner <<-EOS
#{APP_NAME} #{VERSION_COPYRIGHT}

Usage:
  #{APP_ID} [command] [options]

  commands:
#{SUB_COMMANDS.map { |sub_cmd| "    #{sub_cmd}" }.join( "\n" )}

  (For help with a command: #{APP_ID} [command] -h)

options:
  EOS
    stop_on SUB_COMMANDS
  end

  # Get the sub-command and its options
  #
  @cmd = ARGV.shift || ''
  @options = case( cmd )
    when "restart"
      Trollop::options do
        opt :config, "The path for the config file", :type => String, :short => '-c', :default => DEFAULT_CONFIG_PATH
        opt :log_level, "The log level", :type => String, :default => 'info'
        opt :log, "The path for the log file", :type => String, :short => '-l', :default => DEFAULT_LOG_PATH
        opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
        opt :threads, "The number of threads", :type => Integer, :short => '-t'
      end
    when "start"
      Trollop::options do
        opt :config, "The path for the config file", :type => String, :short => '-c', :default => DEFAULT_CONFIG_PATH
        opt :interactive, "Execute the server in interactive mode", :short => '-i'
        opt :log_level, "The log level", :type => String, :default => 'info'
        opt :log, "The path for the log file", :type => String, :short => '-l', :default => DEFAULT_LOG_PATH
        opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
        opt :threads, "The number of threads", :type => Integer, :short => '-t'
      end
    when "status"
      Trollop::options do
        opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
      end
    when "stop"
      Trollop::options do
        opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
      end
    else
      Trollop::die "unknown command #{cmd.inspect}"
    end

  if cmd == 'start'
    unless options[:interactive]
      Trollop::die( :config, "is required when running as daemon" ) unless options[:config]
      Trollop::die( :log, "is required when running as daemon" ) unless options[:log]
      Trollop::die( :pid, "is required when running as daemon" ) unless options[:pid]
    end
  end

  if %w(restart status stop).include?( cmd )
    Trollop::die( :pid, "is required" ) unless options[:pid]
  end
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



9
10
11
# File 'lib/rabbit_wq/cli.rb', line 9

def cmd
  @cmd
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/rabbit_wq/cli.rb', line 9

def options
  @options
end

Instance Method Details

#runObject



88
89
90
# File 'lib/rabbit_wq/cli.rb', line 88

def run
  send( cmd )
end