class PuppetHerald::CLI

A CLI class

Public Class Methods

new() click to toggle source

Initialize CLI @return [CLI] an CLI object

# File lib/puppet-herald/cli.rb, line 12
def initialize
  self
end

Public Instance Methods

run!(argv = ARGV) click to toggle source

Executes an Herald app from CLI

@param argv [Array] an argv from CLI @return [Integer] a status code for program

# File lib/puppet-herald/cli.rb, line 20
def run!(argv = ARGV)
  PuppetHerald.rackenv

  options = parse_or_kill argv, 2
  run_or_kill options, 1
  Kernel.exit 0
end

Protected Instance Methods

parse(argv) click to toggle source

Parse an ARGV command line arguments @param argv [Array] an argv from CLI @return [Hash] options to use by application

# File lib/puppet-herald/cli.rb, line 33
def parse(argv)
  options = parser.process!(argv)

  msg = "Starting #{PuppetHerald::NAME} v#{PuppetHerald::VERSION} in #{PuppetHerald.rackenv}..."
  PuppetHerald.logger.info msg
  PuppetHerald.database.dbconn   = options[:dbconn]
  PuppetHerald.database.passfile = options[:passfile]
  PuppetHerald.database.spec(true)
  options
end

Private Instance Methods

banner() click to toggle source
parse_or_kill(argv, retcode) click to toggle source
# File lib/puppet-herald/cli.rb, line 55
def parse_or_kill(argv, retcode)
  return parse argv
rescue StandardError => ex
  PuppetHerald.errlogger.fatal "Database configuration is invalid!\n\n#{ex.message}"
  Kernel.exit retcode
end
parser() click to toggle source
# File lib/puppet-herald/cli.rb, line 77
def parser
  home = File.expand_path('~')
  defaultdb     = "sqlite://#{home}/pherald.db"
  defaultdbpass = "#{home}/.pherald.pass"
  Parser.new do |p|
    p.banner = banner
    p.version = PuppetHerald::VERSION
    p.option :bind, 'Hostname to bind to', default: 'localhost'
    p.option :port, 'Port to use', default: 11_303, value_satisfies: ->(x) { x >= 1 && x <= 65_535 }
    p.option :dbconn, 'Connection string to database, see info above', default: defaultdb
    p.option(
      :passfile,
      'If using postgresql, this file will be read for password to database',
      default: defaultdbpass
    )
  end
end
run_or_kill(options, retcode) click to toggle source
# File lib/puppet-herald/cli.rb, line 46
def run_or_kill(options, retcode)
  require 'puppet-herald/application'
  PuppetHerald::Application.run! options
rescue StandardError => ex
  bug = PuppetHerald.bug(ex)
  PuppetHerald.errlogger.fatal "Unexpected error occured, mayby a bug?\n\n#{bug[:message]}\n\n#{bug[:help]}"
  Kernel.exit retcode
end