Sha256: b85b00d4bc60b7647c19165a85906fb06f6138c3f79284608d336468b57d57ae

Contents?: true

Size: 1.46 KB

Versions: 33

Compression:

Stored size: 1.46 KB

Contents

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

    def run
      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

        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

33 entries across 33 versions & 1 rubygems

Version Path
terraspace-1.0.6 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.0.5 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.0.4 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.0.3 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.0.2 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.0.1 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-1.0.0 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.7.2 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.7.1 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.7.0 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.23 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.22 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.21 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.20 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.19 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.18 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.17 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.16 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.15 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.6.13 lib/terraspace/compiler/strategy/tfvar.rb