// SaveLocksToFile writes the given locks object to the given file,
// entirely replacing any content already in that file, or returns error
// diagnostics explaining why that was not possible.
//
// SaveLocksToFile attempts an atomic replacement of the file, as an aid
// to external tools such as text editor integrations that might be monitoring
// the file as a signal to invalidate cached metadata. Consequently, other
// temporary files may be temporarily created in the same directory as the
// given filename during the operation.
SaveLocksToFile writes the given locks object to the given file,
entirely replacing any content already in that file, or returns error
diagnostics explaining why that was not possible.

SaveLocksToFile attempts an atomic replacement of the file, as an aid
to external tools such as text editor integrations that might be monitoring
the file as a signal to invalidate cached metadata. Consequently, other
temporary files may be temporarily created in the same directory as the
given filename during the operation.
// SaveLocksToFile writes the given locks object to the given file // entirely replacing any content already in that file or returns error // diagnostics explaining why that was not possible . // // SaveLocksToFile attempts an atomic replacement of the file as an aid // to external tools such as text editor integrations that might be monitoring // the file as a signal to invalidate cached metadata . Consequently other // temporary files may be temporarily created in the same directory as the // given filename during the operation .
func SaveLocksToFile(locks *Locks, filename string) tfdiags.Diagnostics {
	var diags tfdiags.Diagnostics
	f := hclwrite.NewEmptyFile()
	rootBody := f.Body()
	rootBody.AppendUnstructuredTokens(hclwrite.Tokens{
		{
			Type:  hclsyntax.TokenComment,
			Bytes: []byte("# This file is maintained automatically by \"terraform init\".\n"),
		},
		{
			Type:  hclsyntax.TokenComment,
			Bytes: []byte("# Manual edits may be lost in future updates.\n"),
		},
	})
	providers := make([]addrs.Provider, 0, len(locks.providers))
	for provider := range locks.providers {
		providers = append(providers, provider)
	}
	sort.Slice(providers, func(i, j int) bool {
		return providers[i].LessThan(providers[j])
	})
	for _, provider := range providers {
		lock := locks.providers[provider]
		rootBody.AppendNewline()
		block := rootBody.AppendNewBlock("provider", []string{lock.addr.String()})
		body := block.Body()
		body.SetAttributeValue("version", cty.StringVal(lock.version.String()))
		if constraintsStr := getproviders.VersionConstraintsString(lock.versionConstraints); constraintsStr != "" {
			body.SetAttributeValue("constraints", cty.StringVal(constraintsStr))
		}
		if len(lock.hashes) != 0 {
			hashToks := encodeHashSetTokens(lock.hashes)
			body.SetAttributeRaw("hashes", hashToks)
		}
	}
	newContent := f.Bytes()
	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 SaveLocksToFile ( locks * Locks , filename string ) tfdiags . Diagnostics { var diags tfdiags . Diagnostics f := hclwrite . NewEmptyFile ( ) rootBody := f . Body ( ) rootBody . AppendUnstructuredTokens ( hclwrite . Tokens { { Type : hclsyntax . TokenComment , Bytes : [ ] byte ( " \" \" \n " ) , } , { Type : hclsyntax . TokenComment , Bytes : [ ] byte ( " \n " ) , } , } ) providers := make ( [ ] addrs . Provider , 0 , len ( locks . providers ) ) for provider := range locks . providers { providers = append ( providers , provider ) } sort . Slice ( providers , func ( i , j int ) bool { return providers [ i ] . LessThan ( providers [ j ] ) } ) for _ , provider := range providers { lock := locks . providers [ provider ] rootBody . AppendNewline ( ) block := rootBody . AppendNewBlock ( " " , [ ] string { lock . addr . String ( ) } ) body := block . Body ( ) body . SetAttributeValue ( " " , cty . StringVal ( lock . version . String ( ) ) ) if constraintsStr := getproviders . VersionConstraintsString ( lock . versionConstraints ) ; constraintsStr != " " { body . SetAttributeValue ( " " , cty . StringVal ( constraintsStr ) ) } if len ( lock . hashes ) != 0 { hashToks := encodeHashSetTokens ( lock . hashes ) body . SetAttributeRaw ( " " , hashToks ) } } newContent := f . Bytes ( ) err := replacefile . AtomicWriteFile ( filename , newContent , 0644 ) if err != nil { diags = diags . Append ( tfdiags . Sourceless ( tfdiags . Error , " " , fmt . Sprintf ( " " , filename , err ) , ) ) return diags } return diags }
SaveLocksToFile writes the given locks object to the given file,
entirely replacing any content already in that file, or returns error
diagnostics explaining why that was not possible.
SaveLocksToFile writes the given locks object to the given file entirely replacing any content already in that file or returns error diagnostics explaining why that was not possible .
// In other uses of the "hclwrite" package we typically try to make // surgical updates to the author's existing files, preserving their // block ordering, comments, etc. We intentionally don't do that here // to reinforce the fact that this file primarily belongs to Terraform, // and to help ensure that VCS diffs of the file primarily reflect // changes that actually affect functionality rather than just cosmetic // changes, by maintaining it in a highly-normalized form. // End-users _may_ edit the lock file in exceptional situations, like // working around potential dependency selection bugs, but we intend it // to be primarily maintained automatically by the "terraform init" // command.