class Honeybadger::CLI::Main

def self.project_options

def self.project_options
  option :api_key,         required: false, aliases: :'-k', type: :string, desc: 'Api key of your Honeybadger application'
  option :environment,     required: false, aliases: [:'-e', :'-env'], type: :string, desc: 'Environment this command is being executed in (i.e. "production", "staging")'
  option :skip_rails_load, required: false, type: :boolean, desc: 'Flag to skip rails initialization'
end

def build_config(options)

def build_config(options)
  load_env(options)
  config = Honeybadger.config
  config.set(:report_data, true)
  config.set(:api_key, fetch_value(options, 'api_key')) if options.has_key?('api_key')
  config.set(:env, fetch_value(options, 'environment')) if options.has_key?('environment')
  config
end

def deploy

def deploy
  config = build_config(options)
  if config.get(:api_key).to_s =~ BLANK
    say("No value provided for required options '--api-key'")
    exit(1)
  end
  Deploy.new(options, [], config).run
rescue => e
  log_error(e)
  exit(1)
end

def exec(*args)

def exec(*args)
  if args.size == 0
    say("honeybadger: exec needs a command to run", :red)
    exit(1)
  end
  config = build_config(options)
  if config.get(:api_key).to_s =~ BLANK
    say("No value provided for required options '--api-key'", :red)
    exit(1)
  end
  Exec.new(options, args, config).run
rescue => e
  log_error(e)
  exit(1)
end

def fetch_value(options, key)

def fetch_value(options, key)
  options[key] == key ? nil : options[key]
end

def help(*args, &block)

def help(*args, &block)
  if args.size == 0
    say(<<-WELCOME)
eybadger v#{VERSION}
adger is your favorite error tracker for Ruby. When your app raises an
ion we notify you with all the context you need to fix it.
neybadger CLI provides tools for interacting with Honeybadger via the
d line.
 need support, please drop us a line: support@honeybadger.io
E
  end
  super
end

def install(api_key)

def install(api_key)
  Install.new(options, api_key).run
rescue => e
  log_error(e)
  exit(1)
end

def load_env(options)

def load_env(options)
  # Initialize Rails when running from Rails root.
  environment_rb = File.join(Dir.pwd, 'config', 'environment.rb')
  if File.exist?(environment_rb)
    load_rails_env_if_allowed(environment_rb, options)
  end
  # Ensure config is loaded (will be skipped if initialized by Rails).
  Honeybadger.config.load!
end

def load_rails_env(environment_rb)

def load_rails_env(environment_rb)
  begin
    require 'rails'
  rescue LoadError
    # No Rails, so skip loading Rails environment.
    return
  end
  require environment_rb
end

def load_rails_env_if_allowed(environment_rb, options)

def load_rails_env_if_allowed(environment_rb, options)
  # Skip Rails initialization according to option flag
  if options.has_key?('skip_rails_load') && fetch_value(options, 'skip_rails_load')
    say("Skipping Rails initialization.")
  else
    load_rails_env(environment_rb)
  end
end

def log_error(e)

def log_error(e)
  case e
  when *Util::HTTP::ERRORS
    say(<<-MSG, :red)
 Failed to notify Honeybadger ------------------------------------------- !!
 happened?
ncountered an HTTP error while contacting our service. Issues like this are
lly temporary.
r details
class}: #{e.message}\n    at #{e.backtrace && e.backtrace.first}
 can I do?
try the command.
ke sure you can connect to api.honeybadger.io (`curl https://api.honeybadger.io/v1/notices`).
 you continue to see this message, email us at support@honeybadger.io
on't forget to attach this output!)
 End -------------------------------------------------------------------- !!
  else
    say(<<-MSG, :red)
 Honeybadger command failed --------------------------------------------- !!
 did you try to do?
tried to execute the following command:
eybadger #{ARGV.join(' ')}`
 actually happend?
ncountered a Ruby exception and were forced to cancel your request.
r details
class}: #{e.message}
e.backtrace && e.backtrace.join("\n    ")}
 can I do?
 you're calling the `install` or `test` command in a Rails app, make sure
u can boot the Rails console: `bundle exec rails console`.
try the command.
 you continue to see this message, email us at support@honeybadger.io
on't forget to attach this output!)
 End -------------------------------------------------------------------- !!
  end
end

def notify

def notify
  config = build_config(options)
  if config.get(:api_key).to_s =~ BLANK
    say("No value provided for required options '--api-key'")
    exit(1)
  end
  Notify.new(options, [], config).run
rescue => e
  log_error(e)
  exit(1)
end

def test

def test
  Test.new(options).run
rescue => e
  log_error(e)
  exit(1)
end