opentofu/internal/command/plugins_lock.go
Martin Atkins ffe056bacb Move command/ to internal/command/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

64 lines
1.9 KiB
Go

package command
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
)
func (m *Meta) providerPluginsLock() *pluginSHA256LockFile {
return &pluginSHA256LockFile{
Filename: filepath.Join(m.pluginDir(), "lock.json"),
}
}
type pluginSHA256LockFile struct {
Filename string
}
// Read loads the lock information from the file and returns it. If the file
// cannot be read, an empty map is returned to indicate that _no_ providers
// are acceptable, since the user must run "terraform init" to lock some
// providers before a context can be created.
func (pf *pluginSHA256LockFile) Read() map[string][]byte {
// Returning an empty map is different than nil because it causes
// us to reject all plugins as uninitialized, rather than applying no
// constraints at all.
//
// We don't surface any specific errors here because we want it to all
// roll up into our more-user-friendly error that appears when plugin
// constraint verification fails during context creation.
digests := make(map[string][]byte)
buf, err := ioutil.ReadFile(pf.Filename)
if err != nil {
// This is expected if the user runs any context-using command before
// running "terraform init".
log.Printf("[INFO] Failed to read plugin lock file %s: %s", pf.Filename, err)
return digests
}
var strDigests map[string]string
err = json.Unmarshal(buf, &strDigests)
if err != nil {
// This should never happen unless the user directly edits the file.
log.Printf("[WARN] Plugin lock file %s failed to parse as JSON: %s", pf.Filename, err)
return digests
}
for name, strDigest := range strDigests {
var digest []byte
_, err := fmt.Sscanf(strDigest, "%x", &digest)
if err == nil {
digests[name] = digest
} else {
// This should never happen unless the user directly edits the file.
log.Printf("[WARN] Plugin lock file %s has invalid digest for %q", pf.Filename, name)
}
}
return digests
}