module ErbHiera

Constants

VERSION

Attributes

options[RW]
scope[RW]

Public Class Methods

run() click to toggle source
# File lib/erb-hiera.rb, line 18
def self.run
  @options = CLI.parse

  mappings.each do |mapping|
    ErbHiera.scope  = mapping["scope"]
    input           = mapping["dir"]["input"]
    output          = mapping["dir"]["output"]

    [:input, :output].each do |location|
      raise StandardError, "error: undefined #{dir.to_s.split('_')[0]}put" unless binding.local_variable_get(location)
    end

    # if input is a file then out_file is a file too
    if input =~ /.erb$/
      generate(output, input)
      next
    end

    # otherwise the input/output are directories and all files should be processed..
    manifests(input).each do |manifest|
      out_file = File.join(output, manifest.gsub(input, ""))
      generate(out_file, manifest)
    end
  end
rescue => error
  handle_error(error)
  exit 1
end

Private Class Methods

generate(out_file, manifest) click to toggle source
# File lib/erb-hiera.rb, line 49
def self.generate(out_file, manifest)
  Manifest.info(manifest, out_file) if options[:verbose]

  erb = ERB.new(File.read(manifest), nil, "-").result(Hiera.get_binding)

  puts erb if options[:verbose]

  unless options[:dry_run]
    FileUtils.mkdir_p File.dirname(out_file) unless Dir.exists?(File.dirname(out_file))
    File.write(out_file, erb)
  end
end
handle_error(error) click to toggle source
# File lib/erb-hiera.rb, line 62
def self.handle_error(error)
  if options[:debug]
    puts
    puts error.backtrace
  end

  puts
  puts error
end
manifests(dir) click to toggle source
# File lib/erb-hiera.rb, line 76
def self.manifests(dir)
  Dir.glob(File.join(dir, "**", "*")).reject { |file| File.directory? file }
end
mappings() click to toggle source
# File lib/erb-hiera.rb, line 72
def self.mappings
  YAML.load_file(options[:config])
end