Module: Moose::Inventory::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/moose_inventory/config/config.rb

Overview

This Modules manages application-wide configuration options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#_argvObject (readonly)

Returns the value of attribute _argv.



18
19
20
# File 'lib/moose_inventory/config/config.rb', line 18

def _argv
  @_argv
end

#_confoptsObject (readonly)

Returns the value of attribute _confopts.



19
20
21
# File 'lib/moose_inventory/config/config.rb', line 19

def _confopts
  @_confopts
end

#_settingsObject (readonly)

Returns the value of attribute _settings.



20
21
22
# File 'lib/moose_inventory/config/config.rb', line 20

def _settings
  @_settings
end

Class Method Details

.ansible_argsObject




69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/moose_inventory/config/config.rb', line 69

def self.ansible_args  # rubocop:disable Metrics/AbcSize
  # Handle Ansible flags
  # --hosts           => list all hosts
  # --hosts HOSTNAME  => get host name
  # --groups          => list all groups

  if @_argv[0] == '--hosts'
    host = @_argv[1]
    if !host.nil?
      @_argv.clear
      ['host', 'get', "#{host}", '--ansiblestyle'].each do |arg|
        @_argv << arg
      end
    else
      @_argv.clear
      ['host', 'list', '--ansiblestyle'].each do |arg|
        @_argv << arg
      end
    end
  elsif @_argv[0] == '--groups'
    ['group', 'list', '--ansiblestyle'].each do |arg|
      @_argv << arg
    end
  end
end

.init(args) ⇒ Object




23
24
25
26
27
28
29
30
# File 'lib/moose_inventory/config/config.rb', line 23

def self.init(args)
  @_argv = args.dup

  top_level_args
  # ansible_args
  resolve_config_file
  load
end

.loadObject


rubocop:disable PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/moose_inventory/config/config.rb', line 142

def self.load
  newsets = symbolize_keys(YAML.load_file(@_confopts[:config]))

  path = @_confopts[:config]

  # Get the "general" section
  @_settings[:general] = newsets[:general]
  @_settings[:general].nil? && fail("Missing 'general' root in #{path}")

  # Get the config for the correct environment

  if @_confopts[:env] && !@_confopts[:env].empty?
    env = @_confopts[:env]
    @_settings[:config] = newsets[@_confopts[:env].to_sym]
  else
    env  = @_settings[:general][:defaultenv]
    (env.nil? || env.empty?) && fail("No defaultenv set in #{path}")
    @_settings[:config] = newsets[env.to_sym]
  end

  @_settings[:config].nil? && fail("Missing '#{env}' root in #{path}")

  # And now we should have a valid config stuffed into @_options[:config]
end

.resolve_config_fileObject




96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/moose_inventory/config/config.rb', line 96

def self.resolve_config_file # rubocop:disable Metrics/AbcSize
  if ! @_confopts[:config].nil?
    path = File.expand_path(@_confopts[:config])
    if File.exist?(path)
      @_confopts[:config] = path
    else
      fail("The configuration file #{path} does not exist")
    end
  else
    possibles = ['./.moose-tools/inventory/config',
                 '~/.moose-tools/inventory/config',
                 '~/local/etc/moose-tools/inventory/config',
                 '/etc/moose-tools/inventory/config'
                ]
    possibles.each do |f|
      file = File.expand_path(f)
      @_confopts[:config] = file if File.exist?(file)
    end
  end

  if @_confopts[:config].nil?
    fail('No configuration either given or found in standard locations.')
  end
end

.symbolize_keys(hash) ⇒ Object




122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/moose_inventory/config/config.rb', line 122

def self.symbolize_keys(hash)
  # rubocop:disable Style/EachWithObject
  hash.inject({}) do |result, (key, value)|
    new_key = case key
              when String then key.to_sym
              else key
              end
    new_value = case value
                when Hash then symbolize_keys(value)
                else value
                end
    result[new_key] = new_value
    result
  end
  # rubocop:enable Style/EachWithObject
end

.top_level_argsObject




33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/moose_inventory/config/config.rb', line 33

def self.top_level_args
  # Certain top level flags affect the configuration.
  # --config FILE  => sets the configuration file to be used.
  #                   Default is to search standard locations.
  # --env ENV      => sets the section to be used as the configuration.
  #                   Defaults to ""
  # --format FORMAT=> See formatter for supported types.
  #                   Defaults to json.

  @_confopts = { env: '', format: 'json', trace: false }

  # The following are a O(n^m) approach.  TODO: O(n) version?
    
  # Check for two-part flags   
  %w(env config format).each do |var|
    @_argv.each_with_index do |val, index|
      next if val != "--#{var}"
      @_confopts[var.to_sym] = @_argv[index + 1]
      1.downto(0) { |offset| @_argv.delete_at(index + offset) }
      break
    end
  end
  
  # Check for one-part flags
  %w(trace).each do |var|
    @_argv.each_with_index do |val, index|
      next if val != "--#{var}"
      @_confopts[var.to_sym] = true
      @_argv.delete_at(index)
      break
    end
  end
  
end