Sha256: d5c95d6893e86a48eb9c378a3bd566acb7382d1ace859bef80cfce78db95d0a8

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

require "yaml"

module TerraspaceBundler
  class Lockfile
    include Singleton
    include TB::Util::Logging

    # {"vpc"=>
    #   {"sha"=>"52328b2b5197f9b95e3005cfcfb99595040ee45b",
    #   "source"=>"org/terraform-aws-vpc",
    #   "url"=>"https://github.com/org/terraform-aws-vpc"},
    # "instance"=>
    #   {"sha"=>"570cca3ea7b25e3af1961dc57b27ca2c129b934a",
    #   "source"=>"org/terraform-aws-instance",
    #   "url"=>"https://github.com/org/terraform-aws-instance"}}
    @@mods = nil
    def mods
      return @@mods if @@mods
      lockfile = TB.config.lockfile
      mods = File.exist?(lockfile) ? YAML.load_file(lockfile) : []
      @@mods = mods.map do |name, props|
        new_mod(name, props)
      end
    end

    def new_mod(name, props)
      Mod.new(props.merge(name: name))
    end

    # update (if version mismatch) or create (if missing)
    def sync(mod)
      replace!(mod) if changed?(mod)
    end

    # mod built from Terrafile
    def changed?(mod)
      # missing module case
      found = mods.find { |locked_mod| locked_mod.name == mod.name }
      unless found
        logger.debug "Replacing mod: #{mod.name}. Not found in Terrafile.lock"
        return true
      end

      comparer = VersionComparer.new(found, mod)
      comparer.run
      logger.debug("REASON: #{comparer.reason}") if comparer.reason
      comparer.changed?
    end

    def replace!(mod)
      # mods are immediately fresh from writing to @@mods directly
      @@mods.delete_if { |m| m.name == mod.name }
      @@mods << mod
      @@mods.sort_by!(&:name)
    end

    def prune(removed_mods)
      removals = removed_mods.map(&:name)
      @@mods.delete_if { |m| removals.include?(m.name) }
    end

    class << self
      def write
        Yamler.write(@@mods) if @@mods
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
terraspace-bundler-0.5.0 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.4.4 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.4.3 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.4.2 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.4.1 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.4.0 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.3.4 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.3.3 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.3.2 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.3.1 lib/terraspace_bundler/lockfile.rb
terraspace-bundler-0.3.0 lib/terraspace_bundler/lockfile.rb