Sha256: 6a91305fe23cdfe11030a2b4087ae32dff217c1a1ac90c515e792a126591b67e

Contents?: true

Size: 1.14 KB

Versions: 33

Compression:

Stored size: 1.14 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

    # 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.rjust(2, '0') # add leading 0 in case there are more than 10 layers
      name = "#{prefix}-#{File.basename(layer_path)}"
      name.sub('.tfvars','.auto.tfvars')
          .sub('.rb','.auto.tfvars.json')
    end

    def layer_paths
      Layer.new(@mod).paths
    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-0.5.12 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.11 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.10 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.9 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.8 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.7 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.6 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.5 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.4 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.3 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.2 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.1 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.5.0 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.4.4 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.4.3 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.4.2 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.4.1 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.4.0 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.3.6 lib/terraspace/compiler/strategy/tfvar.rb
terraspace-0.3.5 lib/terraspace/compiler/strategy/tfvar.rb