Class: Nadir::CLI

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/nadir/cli.rb

Instance Method Summary collapse

Instance Method Details

#config_file_pathObject



80
81
82
# File 'lib/nadir/cli.rb', line 80

def config_file_path
  File.expand_path 'config/nadir.yml'
end

#runObject



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

def run
  program :name, 'nadir'
  program :version, Nadir::VERSION
  program :description, 'Ruby/Rails client for Nadir'

  never_trace!

  command 'setup' do |c|
    c.syntax = 'nadir setup API-KEY'
    c.description = 'Creates Nadir\'s configuration file config/nadir.yml and populates it with the provided API-KEY.'
    c.action do |args, _options|
      api_key = args[0]

      if File.exist? config_file_path
        say_error 'Nadir config file already exists.'

        exit(0)
      end

      unless api_key
        say_error 'Please provide an API KEY.'

        exit(0)
      end

      configuration_file = <<~YAML
      ---
      api_key: #{api_key}
      YAML

      File.open(config_file_path, 'w') { |file| file.write(configuration_file) }

      say_success <<~MSG
      Nadir client configured successfully!

      Test the integration by sending a test notification:

      bundle exec nadir test-notify

      If all is well, you'll see the test notification in https://nadir.dev.
      MSG
    end
  end

  command 'test-notify' do |c|
    c.syntax = 'nadir test-notify'
    c.description = 'Send a test notification to Nadir'
    c.action do |args, _options|
      require File.expand_path('config/environment')

      Nadir.configure do |config|
        config.enabled_for << 'development'
      end

      begin
        raise NadirTestException.new('Test message')
      rescue NadirTestException => e
        if Nadir.notify e, location: 'nadir test-notify'
          say_success 'Test notification sent successfully.'
        else
          say_error 'Test notification could not be delivered.'
        end

        Nadir::Transport::HTTPAsync.shut_down
      end
    end
  end

  run!
end

#say_error(message) ⇒ Object



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

def say_error(message)
  say color(message, :red)
end

#say_success(message) ⇒ Object



84
85
86
# File 'lib/nadir/cli.rb', line 84

def say_success(message)
  say color(message, :green)
end