depsfile: SaveLocksToBytes function

In a future commit we'll use this to include dependency lock information
in full fidelity inside a saved plan file.
This commit is contained in:
Martin Atkins 2021-09-29 16:13:20 -07:00
parent 6a98e4720c
commit 3f85591998
1 changed files with 28 additions and 13 deletions

View File

@ -43,6 +43,12 @@ func LoadLocksFromFile(filename string) (*Locks, tfdiags.Diagnostics) {
// integration testing (avoiding creating temporary files on disk); if you // integration testing (avoiding creating temporary files on disk); if you
// are writing non-test code, consider whether LoadLocksFromFile might be // are writing non-test code, consider whether LoadLocksFromFile might be
// more appropriate to call. // more appropriate to call.
//
// It is valid to use this with dependency lock information recorded as part of
// a plan file, in which case the given filename will typically be a
// placeholder that will only be seen in the unusual case that the plan file
// contains an invalid lock file, which should only be possible if the user
// edited it directly (Terraform bugs notwithstanding).
func LoadLocksFromBytes(src []byte, filename string) (*Locks, tfdiags.Diagnostics) { func LoadLocksFromBytes(src []byte, filename string) (*Locks, tfdiags.Diagnostics) {
return loadLocks(func(parser *hclparse.Parser) (*hcl.File, hcl.Diagnostics) { return loadLocks(func(parser *hclparse.Parser) (*hcl.File, hcl.Diagnostics) {
return parser.ParseHCL(src, filename) return parser.ParseHCL(src, filename)
@ -80,6 +86,27 @@ func loadLocks(loadParse func(*hclparse.Parser) (*hcl.File, hcl.Diagnostics)) (*
// temporary files may be temporarily created in the same directory as the // temporary files may be temporarily created in the same directory as the
// given filename during the operation. // given filename during the operation.
func SaveLocksToFile(locks *Locks, filename string) tfdiags.Diagnostics { func SaveLocksToFile(locks *Locks, filename string) tfdiags.Diagnostics {
src, diags := SaveLocksToBytes(locks)
if diags.HasErrors() {
return diags
}
err := replacefile.AtomicWriteFile(filename, src, 0644)
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to update dependency lock file",
fmt.Sprintf("Error while writing new dependency lock information to %s: %s.", filename, err),
))
return diags
}
return diags
}
// SaveLocksToBytes writes the given locks object into a byte array,
// using the same syntax that LoadLocksFromBytes expects to parse.
func SaveLocksToBytes(locks *Locks) ([]byte, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
// In other uses of the "hclwrite" package we typically try to make // In other uses of the "hclwrite" package we typically try to make
@ -131,19 +158,7 @@ func SaveLocksToFile(locks *Locks, filename string) tfdiags.Diagnostics {
} }
} }
newContent := f.Bytes() return f.Bytes(), diags
err := replacefile.AtomicWriteFile(filename, newContent, 0644)
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to update dependency lock file",
fmt.Sprintf("Error while writing new dependency lock information to %s: %s.", filename, err),
))
return diags
}
return diags
} }
func decodeLocksFromHCL(locks *Locks, body hcl.Body) tfdiags.Diagnostics { func decodeLocksFromHCL(locks *Locks, body hcl.Body) tfdiags.Diagnostics {