Sha256: 7b47e356be333721ff436f046df724f40a758d13215118e4faefec3a48b3cfb1

Contents?: true

Size: 1.67 KB

Versions: 7

Compression:

Stored size: 1.67 KB

Contents

require 'fileutils'

module Hieracles
  # configuration singleton
  module Config
    extend self

    attr_reader :extraparams, :server, :classpath, 
      :modulepath, :hierafile, :basepath, :encpath, :format

    def load(options)
      @optionfile = options[:config] || defaultconfig
      @extraparams = extract_params(options[:params])
      initconfig(@optionfile) unless File.exist? @optionfile
      values = YAML.load_file(@optionfile)
      @server = values['server']
      @classpath = values['classpath']
      @modulepath = values['modulepath'] || 'modules'
      @encpath = options[:encpath] || values['encpath'] || 'enc'
      @basepath = File.expand_path(options[:basepath] || values['basepath'] || '.')
      @hierafile = options[:hierafile] || values['hierafile'] || 'hiera.yaml'
      @format = (options[:format] || values['format'] || 'console').capitalize
    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
    end

    def defaultconfig
      File.join(ENV['HOME'], '.config', 'hieracles', 'config.yml')
    end

    # str is like: something=xxx;another=yyy  
    def extract_params(str)
      return {} unless str
      str.split(';').reduce({}) do |a, k|
        a["#{k[/^[^=]*/]}".to_sym] = k[/[^=]*$/]
        a
      end
    end

    def path(what)
      File.join(@basepath, send(what.to_sym))
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hieracles-0.1.0 lib/hieracles/config.rb
hieracles-0.0.6 lib/hieracles/config.rb
hieracles-0.0.5 lib/hieracles/config.rb
hieracles-0.0.4 lib/hieracles/config.rb
hieracles-0.0.3 lib/hieracles/config.rb
hieracles-0.0.2 lib/hieracles/config.rb
hieracles-0.0.1 lib/hieracles/config.rb