Sha256: af6391badd2b9d57dbb826f0ada93cb09cd5ee61ae69fd2e1e8f729882c0d1f0

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

require 'fileutils'
require 'TerraformDevKit/template_config_file'

module TerraformDevKit
  class TemplateRenderer
    def initialize(env, project, extra_vars_proc = nil)
      @env = env
      @project = project
      @extra_vars_proc = extra_vars_proc || proc { {} }
    end

    def render_files
      render_files_into_path(Dir['*.mustache'])
      template_dirs = Configuration.get('template-dirs')
      template_dirs.to_h.each do |dest, src|
        render_files_into_path(Dir[File.join(src, '*.mustache')], dest)
      end
    end

    private

    def render_files_into_path(file_list, dest_path = '.')
      aws_config = Configuration.get('aws')
      file_list.each do |fname|
        template_file = TemplateConfigFile.new(
          File.read(fname),
          @project,
          @env,
          aws_config,
          extra_vars: @extra_vars_proc.call(@env)
        )
        config_fname = File.basename(fname, File.extname(fname))
        Dir.chdir(@env.working_dir) do
          FileUtils.makedirs(dest_path)
          config_fname = File.join(dest_path, config_fname)
          File.open(config_fname, 'w') { |f| f.write(template_file.render) }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
TerraformDevKit-0.4.0 lib/TerraformDevKit/template_renderer.rb
TerraformDevKit-0.3.8 lib/TerraformDevKit/template_renderer.rb
TerraformDevKit-0.3.7 lib/TerraformDevKit/template_renderer.rb
TerraformDevKit-0.3.6 lib/TerraformDevKit/template_renderer.rb
TerraformDevKit-0.3.5 lib/TerraformDevKit/template_renderer.rb