kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/configload/module_mgr.go (about)

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