Module: Hieracles::Config

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

Overview

configuration singleton

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#basepathObject (readonly)

Returns the value of attribute basepath.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def basepath
  @basepath
end

#classpathObject (readonly)

Returns the value of attribute classpath.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def classpath
  @classpath
end

#encpathObject (readonly)

Returns the value of attribute encpath.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def encpath
  @encpath
end

#extraparamsObject (readonly)

Returns the value of attribute extraparams.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def extraparams
  @extraparams
end

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def format
  @format
end

#hierafileObject (readonly)

Returns the value of attribute hierafile.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def hierafile
  @hierafile
end

#modulepathObject (readonly)

Returns the value of attribute modulepath.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def modulepath
  @modulepath
end

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/hieracles/config.rb', line 8

def server
  @server
end

Instance Method Details

#defaultconfigObject



38
39
40
# File 'lib/hieracles/config.rb', line 38

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

#extract_params(str) ⇒ Object

str is like: something=xxx;another=yyy



43
44
45
46
47
48
49
# File 'lib/hieracles/config.rb', line 43

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

#initconfig(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hieracles/config.rb', line 25

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

#load(options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hieracles/config.rb', line 11

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

#path(what) ⇒ Object



51
52
53
# File 'lib/hieracles/config.rb', line 51

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