Class: MailRoom::CLI

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

Overview

The CLI parses ARGV into configuration to start the coordinator with.

Author:

  • Tony Pitale

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Initialize a new CLI instance to handle option parsing from arguments

into configuration to start the coordinator running on all mailboxes

Parameters:

  • args (Array)

    `ARGV` passed from `bin/mail_room`


11
12
13
14
15
16
17
18
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
# File 'lib/mail_room/cli.rb', line 11

def initialize(args)
  @options = {}

  OptionParser.new do |parser|
    parser.banner = [
      "Usage: #{@name} [-c config_file]\n",
      "       #{@name} --help\n"
    ].compact.join

    parser.on('-c', '--config FILE') do |path|
      options[:config_path] = path
    end

    parser.on('-q', '--quiet') do
      options[:quiet] = true
    end

    parser.on('--log-exit-as') do |format|
      # accepts 'json' and 'plain'
      # 'plain' is equivalent to no format given
      options[:exit_error_format] = format unless format.nil?
    end

    # parser.on("-l", "--log FILE") do |path|
    #   options[:log_path] = path
    # end

    parser.on_tail("-?", "--help", "Display this usage information.") do
      puts "#{parser}\n"
      exit
    end
  end.parse!(args)

  self.configuration = Configuration.new(options)
  self.coordinator = Coordinator.new(configuration.mailboxes, configuration.health_check)
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.


5
6
7
# File 'lib/mail_room/cli.rb', line 5

def configuration
  @configuration
end

#coordinatorObject

Returns the value of attribute coordinator.


5
6
7
# File 'lib/mail_room/cli.rb', line 5

def coordinator
  @coordinator
end

#optionsObject

Returns the value of attribute options.


5
6
7
# File 'lib/mail_room/cli.rb', line 5

def options
  @options
end

Instance Method Details

#startObject

Start the coordinator running, sets up signal traps


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mail_room/cli.rb', line 49

def start
  Signal.trap(:INT) do
    coordinator.running = false
  end

  Signal.trap(:TERM) do
    exit
  end

  coordinator.run
rescue Exception => e # not just Errors, but includes lower-level Exceptions
  CrashHandler.new.handle(e, @options[:exit_error_format])
  exit
end