Class: MailRunner::CLI

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

Class Method Summary collapse

Class Method Details

.daemonizeObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/mail_runner/cli.rb', line 71

def self.daemonize
  #eq_to Process.daemon
  exit if fork
  Process.setsid
  exit if fork
  Dir.chdir "/" 
  STDIN.reopen "/dev/null"
  STDOUT.reopen "/dev/null", "a" 
  STDERR.reopen "/dev/null", "a" 
end

.initialize_logger(options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/mail_runner/cli.rb', line 82

def self.initialize_logger(options)
  begin
    MailRunner::Logging.initialize_logger(options[:logfile]) if options[:logfile]
    MailRunner::Logging.add_log_file_section_header if options[:logfile]
    MailRunner::Logging.logger.level = ::Logger::DEBUG if options[:verbose]
  rescue => e #primarily to alert invald log path in case of daemon
    puts e.message
    exit 1
  end
end

.initialize_manager_bot(opts) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/mail_runner/cli.rb', line 60

def self.initialize_manager_bot(opts)
 	begin
  bot = MailRunner.initialize_manager_bot
  bot.verify_and_set(opts)
rescue => e
  puts e.message
  exit 1
end
   return bot
end

.parse_config(cf_path) ⇒ Object



56
57
58
# File 'lib/mail_runner/cli.rb', line 56

def self.parse_config(cf_path)
  YAML.load_file(cf_path)
end

.parse_options(argv) ⇒ Object



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
47
48
49
50
51
52
53
54
# File 'lib/mail_runner/cli.rb', line 18

def self.parse_options(argv)
  opts = {}
  @parser = OptionParser.new do |o|
    o.on '-m', '--mailbox MAILBOX', "Name of Mailbox to watch" do |arg|
      opts[:mailbox] = arg
    end

    o.on '-w', '--webhook URL', "Complete url of webhook to deliver mail to" do |arg|
      opts[:webhook] = arg
    end

    o.on '-a', '--archive', "Use config file to setup archive option." do |arg|
      opts[:archive] = arg
    end

    o.on '-d', '--daemon', "Daemonize process. Be sure to add logfile path." do |arg|
      opts[:daemon] = arg
    end

    o.on '-L', '--logfile PATH', "Absolute path to log file." do |arg|
      opts[:logfile] = arg
    end

    o.on '-c', '--config PATH', "Path to YAML config file." do |arg|
      opts[:config] = arg
    end

    o.on '-v', '--verbose', "Logger runs in debug mode." do |arg|
      opts[:verbose] = arg
    end
  end
  @parser.parse!(argv)
  if opts[:config]
    opts = parse_config(opts[:config]).merge(opts)
  end
  opts
end

.set_globalsObject



93
94
95
# File 'lib/mail_runner/cli.rb', line 93

def self.set_globals
  MailRunner.set_globals
end

.start(args) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/mail_runner/cli.rb', line 7

def self.start(args)
  options = parse_options(args)
  initialize_logger(options)
  set_globals
  
  @bot = initialize_manager_bot(options)#run first to make sure it runs bot tests prior to daemonizing a process.
  daemonize if options[:daemon] && options[:daemon] == true
  @bot.run
end