Sha256: c2430d2c3484870f0d3b19160081f3e316bf78d02d069c0d8e7298fbe59cc1ec

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 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 = ":CACHE_ROOT/: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

9 entries across 9 versions & 1 rubygems

Version Path
terraspace-1.1.7 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.6 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.5 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.4 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.3 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.2 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.1 lib/terraspace/compiler/expander/backend.rb
terraspace-1.1.0 lib/terraspace/compiler/expander/backend.rb
terraspace-1.0.6 lib/terraspace/compiler/expander/backend.rb