Class: Wavefront::OptHandler

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/wavefront/opt_handler.rb

Overview

Options to commands can come from three sources, with the following order of precedence: program defaults, a configuration file, and command-line options. Docopt is not well suited to this, as it will “fill in” any missing options with defaults, producing a single hash which must be merged with values from the config file. Assuming we give the command-line higher precedence, a default value, not supplied by the user, will override a value in the config file. The other way round, and you can’t override anything in the config file from the command-line. I think this behaviour is far from unique to Docopt.

So, we have a hash of defaults, and we do the merging ourselves, in this class. We trick Docopt into not using the defaults by avoiding the magic string ‘default: ’ in our options stanzas.

Constant Summary

Constants included from Constants

Constants::ALERT_FORMATS, Constants::DASH_FORMATS, Constants::DEFAULT_ALERT_FORMAT, Constants::DEFAULT_DASH_FORMAT, Constants::DEFAULT_FORMAT, Constants::DEFAULT_HOST, Constants::DEFAULT_INFILE_FORMAT, Constants::DEFAULT_OBSOLETE_METRICS, Constants::DEFAULT_OPTS, Constants::DEFAULT_PERIOD_SECONDS, Constants::DEFAULT_PREFIX_LENGTH, Constants::DEFAULT_PROXY, Constants::DEFAULT_PROXY_PORT, Constants::DEFAULT_SOURCE_FORMAT, Constants::DEFAULT_STRICT, Constants::EVENT_LEVELS, Constants::EVENT_STATE_DIR, Constants::FORMATS, Constants::GRANULARITIES, Constants::SOURCE_FORMATS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf_file, cli_opts = {}) ⇒ OptHandler

Returns a new instance of OptHandler.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wavefront/opt_handler.rb', line 27

def initialize(conf_file, cli_opts = {})
  @conf_file = if cli_opts.key?(:config) && cli_opts[:config]
                 Pathname.new(cli_opts[:config])
               else
                 conf_file
               end

  @cli_opts = cli_opts.reject { |_k, v| v.nil? }

  @opts = DEFAULT_OPTS.merge(load_profile).merge(@cli_opts)
end

Instance Attribute Details

#cli_optsObject (readonly)

Returns the value of attribute cli_opts.



25
26
27
# File 'lib/wavefront/opt_handler.rb', line 25

def cli_opts
  @cli_opts
end

#conf_fileObject (readonly)

Returns the value of attribute conf_file.



25
26
27
# File 'lib/wavefront/opt_handler.rb', line 25

def conf_file
  @conf_file
end

#optsObject (readonly)

Returns the value of attribute opts.



25
26
27
# File 'lib/wavefront/opt_handler.rb', line 25

def opts
  @opts
end

Instance Method Details

#load_profileObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wavefront/opt_handler.rb', line 39

def load_profile
  #
  # Load in configuration options from the (optionally) given
  # section of an ini-style configuration file. If the file's
  # not there, we don't consider that an error. Returns a hash
  # of options which matches what Docopt gives us.
  #
  unless conf_file.exist?
    puts "config file '#{conf_file}' not found. Taking options " \
         'from command-line.'
    return {}
  end

  pf = cli_opts.fetch(:profile, 'default')

  puts "reading '#{pf}' profile from '#{conf_file}'" if cli_opts[:debug]

  IniFile.load(conf_file)[pf].each_with_object({}) do |(k, v), memo|
    memo[k.to_sym] = v
  end
end