summaryrefslogtreecommitdiff
path: root/lib/oxidized/cli.rb
blob: 1e6cf052ca4697154a4303a9b42f298f9942fb4a (plain)
1
2
3
4
5
6
7
8
9
10
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
module Oxidized
  class CLI
    require 'oxidized'
    require 'slop'
    class CLIError < OxidizedError; end
    class NoConfig < CLIError; end

    def run
      Process.daemon unless CFG.debug
      begin
        Oxidized.new
      rescue => error
        crash error
        raise
      end
    end

    private

    def initialize
      raise NoConfig, 'edit ~/.config/oxidized/config' if CFGS.create
      _args, opts = parse_opts
      CFG.debug = true if opts[:debug]
    end

    def crash error
      open Config::Crash, 'w' do |file|
        file.puts '-' * 50
        file.puts Time.now.utc
        file.puts error.message + ' [' + error.class.to_s + ']'
        file.puts '-' * 50
        file.puts error.backtrace
        file.puts '-' * 50
      end
    end

    def parse_opts
      opts = Slop.new(:help=>true) do
        on 'd', 'debug', 'turn on debugging'
      end
      [opts.parse!, opts]
    end
  end
end