Class: Appsignal::CLI

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

Constant Summary collapse

AVAILABLE_COMMANDS =
%w(notify_of_deploy api_check).freeze
PROJECT_ROOT =
ENV['PWD']

Class Method Summary collapse

Class Method Details

.api_checkObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/appsignal/cli.rb', line 114

def api_check
  puts "\nReading config/appsignal.yml and attempting to use the config "\
    "in order to check if it is set up the way it should be.\n\n"
  Appsignal::Config.new(
    PROJECT_ROOT, '', logger
  ).load_all.each do |env, config|
    auth_check = ::Appsignal::AuthCheck.new(
      env,
      {:config => config, :logger => logger}
    )
    puts "[#{env}]"
    puts '  * Configured not to monitor this environment' unless config[:active]
    status, result = auth_check.perform_with_result
    puts "  * #{result}"
  end
end

.command_option_parser(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/appsignal/cli.rb', line 69

def command_option_parser(options)
  {
    'notify_of_deploy' => OptionParser.new do |o|
      o.banner = 'Usage: appsignal notify_of_deploy [options]'

      o.on '--revision=<revision>', "The revision you're deploying" do |arg|
        options[:revision] = arg
      end

      o.on '--repository=<repository>', "The location of the main code repository" do |arg|
        options[:repository] = arg
      end

      o.on '--user=<user>', "The name of the user that's deploying" do |arg|
        options[:user] = arg
      end

      o.on '--environment=<rails_env>', "The environment you're deploying to" do |arg|
        options[:environment] = arg
      end
    end,
    'api_check' => OptionParser.new do |o|
      o.banner = %q(Usage: appsignal api_check

      This command checks the config file in config/appsignal.yml
      and tries to use the api_keys available in each environment to
      see if they work.)
    end
  }
end

.global_option_parser(options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/appsignal/cli.rb', line 50

def global_option_parser(options)
  OptionParser.new do |o|
    o.banner = 'Usage: appsignal <command> [options]'

    o.on '-v', '--version', "Print version and exit" do |arg|
      puts "Appsignal #{Appsignal::VERSION}"
      exit(0)
    end

    o.on '-h', '--help', "Show help and exit" do
      puts o
      exit(0)
    end

    o.separator ''
    o.separator "Available commands: #{AVAILABLE_COMMANDS.join(', ')}"
  end
end

.loggerObject



46
47
48
# File 'lib/appsignal/cli.rb', line 46

def logger
  Logger.new($stdout)
end

.notify_of_deploy(options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/appsignal/cli.rb', line 100

def notify_of_deploy(options)
  validate_required_options([:revision, :repository, :user, :environment], options)
  Appsignal::Marker.new(
    {
      :revision => options[:revision],
      :repository => options[:repository],
      :user => options[:user]
    },
    PROJECT_ROOT,
    options[:environment],
    logger
  ).transmit
end

.run(argv = ARGV) ⇒ Object



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

def run(argv=ARGV)
  unless File.exists?(File.join(PROJECT_ROOT, 'config/appsignal.yml'))
    puts 'No config file present at config/appsignal.yml'
    puts 'Log in to https://appsignal.com to get instructions on how to '\
      'generate the config file.'
    exit(1)
  end
  options = {}
  global = global_option_parser(options)
  commands = command_option_parser(options)

  global.order!(argv)
  command = argv.shift
  if command
    if AVAILABLE_COMMANDS.include?(command)
      commands[command].parse!(argv)
      case command.to_sym
      when :notify_of_deploy
        notify_of_deploy(options)
      when :api_check
        api_check
      end
    else
      puts "Command '#{command}' does not exist, run appsignal -h to "\
        "see the help"
      exit(1)
    end
  else
    # Print help
    puts global
    exit(0)
  end
end