github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/configs/configload/module_mgr.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package configload 5 6 import ( 7 "os" 8 "path/filepath" 9 10 "github.com/hashicorp/terraform-svchost/disco" 11 "github.com/spf13/afero" 12 "github.com/terramate-io/tf/modsdir" 13 "github.com/terramate-io/tf/registry" 14 ) 15 16 type moduleMgr struct { 17 FS afero.Afero 18 19 // CanInstall is true for a module manager that can support installation. 20 // 21 // This must be set only if FS is an afero.OsFs, because the installer 22 // (which uses go-getter) is not aware of the virtual filesystem 23 // abstraction and will always write into the "real" filesystem. 24 CanInstall bool 25 26 // Dir is the path where descendent modules are (or will be) installed. 27 Dir string 28 29 // Services is a service discovery client that will be used to find 30 // remote module registry endpoints. This object may be pre-loaded with 31 // cached discovery information. 32 Services *disco.Disco 33 34 // Registry is a client for the module registry protocol, which is used 35 // when a module is requested from a registry source. 36 Registry *registry.Client 37 38 // manifest tracks the currently-installed modules for this manager. 39 // 40 // The loader may read this. Only the installer may write to it, and 41 // after a set of updates are completed the installer must call 42 // writeModuleManifestSnapshot to persist a snapshot of the manifest 43 // to disk for use on subsequent runs. 44 manifest modsdir.Manifest 45 } 46 47 func (m *moduleMgr) manifestSnapshotPath() string { 48 return filepath.Join(m.Dir, modsdir.ManifestSnapshotFilename) 49 } 50 51 // readModuleManifestSnapshot loads a manifest snapshot from the filesystem. 52 func (m *moduleMgr) readModuleManifestSnapshot() error { 53 r, err := m.FS.Open(m.manifestSnapshotPath()) 54 if err != nil { 55 if os.IsNotExist(err) { 56 // We'll treat a missing file as an empty manifest 57 m.manifest = make(modsdir.Manifest) 58 return nil 59 } 60 return err 61 } 62 63 m.manifest, err = modsdir.ReadManifestSnapshot(r) 64 return err 65 }