Sha256: a7b83efd2e42b441c62f01b362bd7a3bcf01971920cc5de5a8c08d6b46be2cd3

Contents?: true

Size: 1.56 KB

Versions: 30

Compression:

Stored size: 1.56 KB

Contents

# Simpler than Terraspace::Terraform::Runner::Backend::Parser because
# Terraspace::Compiler::Expander autodetect backend super early on.
# It's so early that don't want helper methods like <%= expansion(...) %> to be called.
# Calling the expansion helper itself results in Terraspace autodetecting a concrete
# Terraspace Plugin Expander, which creates an infinite loop.
# This simple detection class avoids calling ERB and avoids the infinite loop.
class Terraspace::Compiler::Expander
  class Backend
    extend Memoist

    def initialize(mod)
      @mod = mod
    end

    COMMENT = /^\s*#/
    # Works for both backend.rb DSL and backend.tf ERB
    def detect
      return nil unless src_path # no backend file. returning nil means a local backend
      lines = IO.readlines(src_path)
      backend_line = lines.find { |l| l.include?("backend") && l !~ COMMENT }
      md = backend_line.match(/['"](.*)['"]/)
      md[1] if md
    end

  private
    # Use original unrendered source as wont know the
    # @mod.cache_dir = ":REGION/:ENV/:BUILD_DIR"
    # Until the concrete Terraspace Plugin Expander has been autodetected.
    # Follow same precedence rules as rest of Terraspace.
    def src_path
      exprs = [
        "app/stacks/#{@mod.build_dir}/backend.*",
        "app/modules/#{@mod.build_dir}/backend.*",
        "vendor/stacks/#{@mod.build_dir}/backend.*",
        "vendor/modules/#{@mod.build_dir}/backend.*",
        "config/terraform/backend.*",
      ]
      path = nil
      exprs.find { |expr| path = Dir.glob(expr).first }
      path
    end
    memoize :src_path
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.16 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.15 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.14 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.13 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.12 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.11 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.10 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.9 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.8 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.7 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.6 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.5 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.4 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.3 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.2 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.1 lib/terraspace/compiler/expander/backend.rb
terraspace-2.2.0 lib/terraspace/compiler/expander/backend.rb
terraspace-2.1.7 lib/terraspace/compiler/expander/backend.rb
terraspace-2.1.6 lib/terraspace/compiler/expander/backend.rb