class Hieracles::Config

Attributes

basepath[R]
classpath[R]
encpath[R]
extraparams[R]
format[R]
hierafile[R]
interactive[R]
modulepath[R]
puppetdb[R]
scope[R]
server[R]
usedb[R]

Public Class Methods

new(options) click to toggle source
# File lib/hieracles/config.rb, line 13
def initialize(options)
  @options = options
  @optionfile = @options[:config] || defaultconfig
  @extraparams = extract_params(options[:params])
  @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

Public Instance Methods

build_path(path) click to toggle source
# File lib/hieracles/config.rb, line 95
def build_path(path)
  File.expand_path(File.join(@basepath, path))
end
defaultconfig() click to toggle source
# File lib/hieracles/config.rb, line 58
def defaultconfig
  File.join(ENV['HOME'], '.config', 'hieracles', 'config.yml')
end
extract_params(str) click to toggle source

str is like: something=xxx;another=yyy

# File lib/hieracles/config.rb, line 63
def extract_params(str)
  return {} unless str
  str.split(',').reduce({}) do |a, k|
    a["#{k[/^[^=]*/]}".to_sym] = k[/[^=]*$/]
    a
  end
end
get_config(file) click to toggle source
# File lib/hieracles/config.rb, line 42
def get_config(file)
  initconfig(file) unless File.exist? file
  YAML.load_file(file)
end
initconfig(file) click to toggle source
# File lib/hieracles/config.rb, line 47
def initconfig(file)
  FileUtils.mkdir_p(File.dirname(file))
  File.open(file, 'w') do |f|
    f.puts '---'
    f.puts 'classpath: manifests/classes/%s.pp'
    f.puts 'modulepath: modules'
    f.puts 'encpath: enc'
    f.puts 'hierafile: hiera.yaml'
  end
end
load_facts(file, format) click to toggle source
# File lib/hieracles/config.rb, line 77
def load_facts(file, format)
  if format == :json
    JSON.parse(File.read(file))
  else
    YAML.load_file(file)
  end
end
load_scope(values) click to toggle source
# File lib/hieracles/config.rb, line 71
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
pick_first(label, default) click to toggle source
# File lib/hieracles/config.rb, line 38
def pick_first(label, default)
  @options[label] || @values[label.to_s] || default
end
resolve_path(path) click to toggle source
# File lib/hieracles/config.rb, line 85
def resolve_path(path)
  if File.exists?(File.expand_path(path))
    File.expand_path(path)
  elsif File.exists?(File.expand_path(File.join(@basepath, path)))
    File.expand_path(File.join(@basepath, path))
  else
    raise IOError, "File #{path} not found."
  end
end