Module: RFacter::Config

Defined in:
lib/rfacter/config.rb,
lib/rfacter.rb

Overview

Stores and sets global configuration

This module stores a global instance of Settings and contains methods for initializing the settings instance from various sources.

Since:

  • 0.1.0

Defined Under Namespace

Classes: Settings

Class Method Summary collapse

Class Method Details

.configRFacter::Config::Settings

Return global configuration

Returns:

Since:

  • 0.1.0



22
23
24
# File 'lib/rfacter/config.rb', line 22

def self.config
  @settings ||= RFacter::Config::Settings.new
end

.configure_from_argv!(argv) ⇒ Array<string>

Set global configuration from an argument vector

This method calls parse_argv and uses the results to update the settings instance returned by config.

Parameters:

  • argv (Array<String>)

    A list of strings passed as command line arguments.

Returns:

  • (Array<string>)

    An array of command line arguments that were not consumed by the parser.

Since:

  • 0.1.0



36
37
38
39
40
# File 'lib/rfacter/config.rb', line 36

def self.configure_from_argv!(argv)
  args, _ = parse_argv(argv, self.config)

  args
end

.parse_argv(argv, settings = nil) ⇒ Array<Array<String>, RFacter::Config::Settings>

Configure a settings instance by parsing an argument vector

Parameters:

  • argv (Array<String>)

    Command line arguments as an array of strings.

  • settings (RFacter::Config::Settings, nil) (defaults to: nil)

    A settings object to configure. A new object will be created if nothing is passed.

Returns:

  • (Array<Array<String>, RFacter::Config::Settings>)

    ] A tuple containing a configured instance of Settings followed by an array of command line arguments that were not consumed by the parser.

Since:

  • 0.1.0



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rfacter/config.rb', line 54

def self.parse_argv(argv, settings = nil)
  settings ||= RFacter::Config::Settings.new
  parser = OptionParser.new
  args = argv.dup

  parser.separator("\nOptions\n=======")

  parser.on('--version', 'Print version number and exit.') do
    puts RFacter::VERSION
    exit 0
  end

  parser.on('-h', '--help', 'Print this help message.') do
    puts parser.help
    exit 0
  end

  parser.on('-v', '--verbose', 'Raise log level to INFO.') do
    settings.logger.level = Logger::INFO
  end

  parser.on('-d', '--debug', 'Raise log level to DEBUG.') do
    settings.logger.level = Logger::DEBUG
  end

  parser.on('--profile', 'Profile fact resolution times.') do
    settings.profile = true
  end

  parser.on('-n', '--node', '=MANDATORY', URI, 'Add a node by URI.') do |uri|
    node = RFacter::Node.new(uri)
    settings.nodes[node.hostname] = node
  end

  parser.parse!(args)

  [args, settings]
end