github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/states/statefile/file.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package statefile 5 6 import ( 7 version "github.com/hashicorp/go-version" 8 9 "github.com/terramate-io/tf/states" 10 tfversion "github.com/terramate-io/tf/version" 11 ) 12 13 // File is the in-memory representation of a state file. It includes the state 14 // itself along with various metadata used to track changing state files for 15 // the same configuration over time. 16 type File struct { 17 // TerraformVersion is the version of Terraform that wrote this state file. 18 TerraformVersion *version.Version 19 20 // Serial is incremented on any operation that modifies 21 // the State file. It is used to detect potentially conflicting 22 // updates. 23 Serial uint64 24 25 // Lineage is set when a new, blank state file is created and then 26 // never updated. This allows us to determine whether the serials 27 // of two states can be meaningfully compared. 28 // Apart from the guarantee that collisions between two lineages 29 // are very unlikely, this value is opaque and external callers 30 // should only compare lineage strings byte-for-byte for equality. 31 Lineage string 32 33 // State is the actual state represented by this file. 34 State *states.State 35 } 36 37 func New(state *states.State, lineage string, serial uint64) *File { 38 // To make life easier on callers, we'll accept a nil state here and just 39 // allocate an empty one, which is required for this file to be successfully 40 // written out. 41 if state == nil { 42 state = states.NewState() 43 } 44 45 return &File{ 46 TerraformVersion: tfversion.SemVer, 47 State: state, 48 Lineage: lineage, 49 Serial: serial, 50 } 51 } 52 53 // DeepCopy is a convenience method to create a new File object whose state 54 // is a deep copy of the receiver's, as implemented by states.State.DeepCopy. 55 func (f *File) DeepCopy() *File { 56 if f == nil { 57 return nil 58 } 59 return &File{ 60 TerraformVersion: f.TerraformVersion, 61 Serial: f.Serial, 62 Lineage: f.Lineage, 63 State: f.State.DeepCopy(), 64 } 65 }