Sha256: 6b30528fa587cfff0eed7f316faccbd7648984e8132c7c4221edfdf77dbe5c35

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module Terraspace::Compiler::Strategy
  class Tfvar
    def initialize(mod)
      @mod = mod
      @order = 0
    end

    def run(write: true)
      layer_paths.each do |layer_path|
        ext = File.extname(layer_path).sub('.','')
        klass = strategy_class(ext)
        next unless klass

        strategy = klass.new(@mod, layer_path)
        content = strategy.run

        next unless write
        dest_name = ordered_name(layer_path)
        writer = Terraspace::Compiler::Writer.new(@mod, dest_name: dest_name)
        writer.write(content)
      end
    end

    def layer_paths
      Layer.new(@mod).paths
    end

    # Tact on number to ensure that tfvars will be processed in desired order.
    # Also name auto.tfvars so it will automatically load
    def ordered_name(layer_path)
      @order += 1
      prefix = @order.to_s
      # add leading 0 when more than 10 layers
      prefix = prefix.rjust(2, '0') if layer_paths.size > 9
      name = "#{prefix}-#{tfvar_name(layer_path)}"
      name.sub('.tfvars','.auto.tfvars')
          .sub('.rb','.auto.tfvars.json')
    end

    def tfvar_name(layer_path)
      if layer_path.include?('/tfvars/')
        name = layer_path.sub(%r{.*/tfvars/},'').gsub('/','-')
        name = "project-#{name}" if layer_path.include?("config/terraform/tfvars")
        name
      else
        File.basename(layer_path)
      end
    end

    def strategy_class(ext)
      "Terraspace::Compiler::Strategy::Tfvar::#{ext.camelize}".constantize
    rescue NameError
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
terraspace-1.1.4 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.1.3 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.1.2 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.1.1 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.1.0 lib/terraspace/compiler/strategy/tfvar.rb