internal/command: remove unused type pluginSHA256LockFile (#575)

Signed-off-by: Lars Lehtonen <lars.lehtonen@gmail.com>
This commit is contained in:
Lars Lehtonen 2023-09-25 08:17:17 -07:00 committed by GitHub
parent 91ced8c2c1
commit 656ab5a8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 88 deletions

View File

@ -1,59 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package command
import (
"encoding/json"
"fmt"
"log"
"os"
)
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 := os.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
}

View File

@ -1,29 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package command
import (
"os"
"reflect"
"testing"
)
func TestPluginSHA256LockFile_Read(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "tf-pluginsha1lockfile-test-")
if err != nil {
t.Fatalf("failed to create temporary file: %s", err)
}
f.Close()
defer os.Remove(f.Name())
plf := &pluginSHA256LockFile{
Filename: f.Name(),
}
// Initially the file is invalid, so we should get an empty map.
digests := plf.Read()
if !reflect.DeepEqual(digests, map[string][]byte{}) {
t.Errorf("wrong initial content %#v; want empty map", digests)
}
}