module PagerdutyCli::Common

Common functionality to pagerduty interaction

Constants

API_KEY_FILE

Public Instance Methods

croak(message) click to toggle source
# File lib/pagerduty_cli/common.rb, line 12
def croak(message)
  $stderr.puts "#{@me} Error: #{message} Exiting."
  exit 1
end
incident_file() click to toggle source

return the name for our incident state file.

# File lib/pagerduty_cli/common.rb, line 27
def incident_file
  File.join(@options[:tmpdir], "pagerduty-#{incident_key}")
end
incident_key() click to toggle source
# File lib/pagerduty_cli/common.rb, line 21
def incident_key
  event_key = "#{@options[:host]}:#{@options[:event]}"
  Digest::SHA1.hexdigest(event_key)
end
load_data() click to toggle source
# File lib/pagerduty_cli/common.rb, line 6
def load_data
  @api_key = File.open(@options[:api_key_file]).read.chomp
rescue Errno::ENOENT, Errno::EPERM
  croak("Could not open PD API key file at #{@options[:api_key_file]}.")
end
parse_common_options(opts) click to toggle source

Disabling the MethodLength cop here because any reduction would reduce readability. rubocop:disable MethodLength

# File lib/pagerduty_cli/common.rb, line 34
def parse_common_options(opts)
  opts.banner = "Usage: #{@me} [options]"
  @options ||= {}
  @options.merge!(host: ENV['HOSTNAME'],
                  api_key_file: API_KEY_FILE,
                  tmpdir: '/tmp')
  opts.on('-H', '--host HOST', 'Report from the hostname given') do |h|
    @options[:host] = h
  end
  opts.on('-k', '--keyfile KEYFILE',
          'Use the key specified in file KEYFILE') do |kf|
    @options[:api_key_file] = kf
          end
  opts.on('-e', '--event EVENT', 'Report the event given') do |e|
    @options[:event] = e
  end
  opts.on('-t', '--tmpdir PATH',
          'location for incident files') do |tmp|
    @options[:tmpdir] = tmp
  end
  opts.on_tail('-h', '--help', 'Show this message') do
    $stderr.puts opts
    exit
  end
end
require_event() click to toggle source

croak if required options are not present

# File lib/pagerduty_cli/common.rb, line 62
def require_event
  return if @options[:event]
  croak('Error: event option is required. Run with -h to see usage.')
end
warn(message) click to toggle source
# File lib/pagerduty_cli/common.rb, line 17
def warn(message)
  $stderr.puts "#{@me} Warning: #{message}"
end