github.com/opentofu/opentofu@v1.7.1/internal/configs/configload/module_mgr.go (about)

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