lib/hieracles/config.rb in hieracles-0.2.0 vs lib/hieracles/config.rb in hieracles-0.2.1

- old
+ new

@@ -7,41 +7,51 @@ # configuration singleton module Config include Hieracles::Utils extend self - attr_reader :extraparams, :server, :classpath, :scope, + attr_reader :extraparams, :server, :classpath, :scope, :puppetdb, :usedb, :modulepath, :hierafile, :basepath, :encpath, :format, :interactive def load(options) - @optionfile = options[:config] || defaultconfig + @options = options + @optionfile = @options[:config] || defaultconfig @extraparams = extract_params(options[:params]) - values = get_config(@optionfile) - @server = values['server'] - @basepath = File.expand_path(options[:basepath] || values['basepath'] || values['localpath'] || '.') - @classpath = build_path(values['classpath']) - @modulepath = resolve_path(values['modulepath'] || 'modules') - @encpath = resolve_path(options[:encpath] || values['encpath'] || 'enc') - @hierafile = resolve_path(options[:hierafile] || values['hierafile'] || 'hiera.yaml') - @format = (options[:format] || values['format'] || 'console').capitalize - facts_file = options[:yaml_facts] || options[:json_facts] - facts_format = options[:json_facts] ? :json : :yaml - @scope = sym_keys((facts_file && load_facts(facts_file, facts_format)) || values['defaultscope'] || {}) - @interactive = options[:interactive] || values['interactive'] + @values = get_config(@optionfile) + @server = @values['server'] + @usedb = if @options[:db] + true + elsif @options[:nodb] + false + else + @values['usedb'] + end + @puppetdb = @values['puppetdb'] + @values['basepath'] ||= @values['localpath'] + @basepath = File.expand_path(pick_first(:basepath, '.')) + @classpath = build_path(@values['classpath']) + @modulepath = resolve_path(pick_first(:modulepath, 'modules')) + @encpath = resolve_path(pick_first(:encpath, 'enc')) + @hierafile = resolve_path(pick_first(:hierafile, 'hiera.yaml')) + @format = pick_first(:format, 'console').capitalize + @scope = load_scope(@values) + @interactive = pick_first(:interactive, false) end + def pick_first(label, default) + @options[label] || @values[label.to_s] || default + end + def get_config(file) initconfig(file) unless File.exist? file - values = YAML.load_file(file) + YAML.load_file(file) end def initconfig(file) FileUtils.mkdir_p(File.dirname(file)) File.open(file, 'w') do |f| f.puts '---' - f.puts '# uncomment if you use the CGI method for discovery' - f.puts '# server: puppetserver.example.com' f.puts 'classpath: manifests/classes/%s.pp' f.puts 'modulepath: modules' f.puts 'encpath: enc' f.puts 'hierafile: hiera.yaml' end @@ -58,11 +68,13 @@ a["#{k[/^[^=]*/]}".to_sym] = k[/[^=]*$/] a end end - def path(what) - send(what.to_sym) + def load_scope(values) + facts_file = @options[:yaml_facts] || @options[:json_facts] + facts_format = @options[:json_facts] ? :json : :yaml + sym_keys((facts_file && load_facts(facts_file, facts_format)) || values['defaultscope'] || {}) end def load_facts(file, format) if format == :json JSON.parse(File.read(file))