github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/plugins_lock.go (about) 1 package command 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "log" 8 ) 9 10 type pluginSHA256LockFile struct { 11 Filename string 12 } 13 14 // Read loads the lock information from the file and returns it. If the file 15 // cannot be read, an empty map is returned to indicate that _no_ providers 16 // are acceptable, since the user must run "terraform init" to lock some 17 // providers before a context can be created. 18 func (pf *pluginSHA256LockFile) Read() map[string][]byte { 19 // Returning an empty map is different than nil because it causes 20 // us to reject all plugins as uninitialized, rather than applying no 21 // constraints at all. 22 // 23 // We don't surface any specific errors here because we want it to all 24 // roll up into our more-user-friendly error that appears when plugin 25 // constraint verification fails during context creation. 26 digests := make(map[string][]byte) 27 28 buf, err := ioutil.ReadFile(pf.Filename) 29 if err != nil { 30 // This is expected if the user runs any context-using command before 31 // running "terraform init". 32 log.Printf("[INFO] Failed to read plugin lock file %s: %s", pf.Filename, err) 33 return digests 34 } 35 36 var strDigests map[string]string 37 err = json.Unmarshal(buf, &strDigests) 38 if err != nil { 39 // This should never happen unless the user directly edits the file. 40 log.Printf("[WARN] Plugin lock file %s failed to parse as JSON: %s", pf.Filename, err) 41 return digests 42 } 43 44 for name, strDigest := range strDigests { 45 var digest []byte 46 _, err := fmt.Sscanf(strDigest, "%x", &digest) 47 if err == nil { 48 digests[name] = digest 49 } else { 50 // This should never happen unless the user directly edits the file. 51 log.Printf("[WARN] Plugin lock file %s has invalid digest for %q", pf.Filename, name) 52 } 53 } 54 55 return digests 56 }