class Honeybadger::CLI::Exec

Constants

FAILED_TEMPLATE
NOT_FOUND_TEMPLATE
NO_EXEC_TEMPLATE

Attributes

args[R]
config[R]
options[R]

Public Class Methods

new(options, args, config) click to toggle source
# File lib/honeybadger/cli/exec.rb, line 46
def initialize(options, args, config)
  @options = options
  @args = args
  @config = config
  @shell = ::Thor::Base.shell.new
end

Public Instance Methods

run() click to toggle source
# File lib/honeybadger/cli/exec.rb, line 53
def run
  result = exec_cmd
  return if result.success

  executable = args.first.to_s[/\S+/]
  payload = {
    api_key: config.get(:api_key),
    notifier: NOTIFIER,
    error: {
      class: 'honeybadger exec error',
      message: result.msg
    },
    request: {
      component: executable,
      context: {
        command: args.join(' '),
        code: result.code,
        pid: result.pid,
        pwd: Dir.pwd,
        path: ENV['PATH']
      }
    },
    server: {
      project_root: Dir.pwd,
      environment_name: config.get(:env),
      time: Time.now,
      stats: Util::Stats.all
    }
  }

  begin
    response = config.backend.notify(:notices, payload)
  rescue
    say(result.msg)
    raise
  end

  if !response.success?
    say(result.msg)
    say(error_message(response), :red)
    exit(1)
  end

  unless quiet?
    say(result.msg)
    say("\nSuccessfully notified Honeybadger")
  end

  exit(0)
end

Private Instance Methods

exec_cmd() click to toggle source
# File lib/honeybadger/cli/exec.rb, line 114
def exec_cmd
  stdout, stderr, status = Open3.capture3(args.join(' '))

  success = status.success? && stderr =~ BLANK
  pid = status.pid
  code = status.to_i
  msg = ERB.new(FAILED_TEMPLATE).result(binding) unless success

  OpenStruct.new(
    msg: msg,
    pid: pid,
    code: code,
    stdout: stdout,
    stderr: stderr,
    success: success
  )
rescue Errno::EACCES, Errno::ENOEXEC
  OpenStruct.new(
    msg: ERB.new(NO_EXEC_TEMPLATE).result(binding),
    code: 126
  )
rescue Errno::ENOENT
  OpenStruct.new(
    msg: ERB.new(NOT_FOUND_TEMPLATE).result(binding),
    code: 127
  )
end
quiet?() click to toggle source
# File lib/honeybadger/cli/exec.rb, line 110
def quiet?
  !!options[:quiet]
end