github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/plans/planfile/wrapped.go (about) 1 package planfile 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/terramate-io/tf/cloud/cloudplan" 8 ) 9 10 // WrappedPlanFile is a sum type that represents a saved plan, loaded from a 11 // file path passed on the command line. If the specified file was a thick local 12 // plan file, the Local field will be populated; if it was a bookmark for a 13 // remote cloud plan, the Cloud field will be populated. In both cases, the 14 // other field is expected to be nil. Finally, the outer struct is also expected 15 // to be used as a pointer, so that a nil value can represent the absence of any 16 // plan file. 17 type WrappedPlanFile struct { 18 local *Reader 19 cloud *cloudplan.SavedPlanBookmark 20 } 21 22 func (w *WrappedPlanFile) IsLocal() bool { 23 return w != nil && w.local != nil 24 } 25 26 func (w *WrappedPlanFile) IsCloud() bool { 27 return w != nil && w.cloud != nil 28 } 29 30 // Local checks whether the wrapped value is a local plan file, and returns it if available. 31 func (w *WrappedPlanFile) Local() (*Reader, bool) { 32 if w != nil && w.local != nil { 33 return w.local, true 34 } else { 35 return nil, false 36 } 37 } 38 39 // Cloud checks whether the wrapped value is a cloud plan file, and returns it if available. 40 func (w *WrappedPlanFile) Cloud() (*cloudplan.SavedPlanBookmark, bool) { 41 if w != nil && w.cloud != nil { 42 return w.cloud, true 43 } else { 44 return nil, false 45 } 46 } 47 48 // NewWrappedLocal constructs a WrappedPlanFile from an already loaded local 49 // plan file reader. Most cases should use OpenWrapped to load from disk 50 // instead. If the provided reader is nil, the returned pointer is nil. 51 func NewWrappedLocal(l *Reader) *WrappedPlanFile { 52 if l != nil { 53 return &WrappedPlanFile{local: l} 54 } else { 55 return nil 56 } 57 } 58 59 // NewWrappedCloud constructs a WrappedPlanFile from an already loaded cloud 60 // plan file. Most cases should use OpenWrapped to load from disk 61 // instead. If the provided plan file is nil, the returned pointer is nil. 62 func NewWrappedCloud(c *cloudplan.SavedPlanBookmark) *WrappedPlanFile { 63 if c != nil { 64 return &WrappedPlanFile{cloud: c} 65 } else { 66 return nil 67 } 68 } 69 70 // OpenWrapped loads a local or cloud plan file from a specified file path, or 71 // returns an error if the file doesn't seem to be a plan file of either kind. 72 // Most consumers should use this and switch behaviors based on the kind of plan 73 // they expected, rather than directly using Open. 74 func OpenWrapped(filename string) (*WrappedPlanFile, error) { 75 // First, try to load it as a local planfile. 76 local, localErr := Open(filename) 77 if localErr == nil { 78 return &WrappedPlanFile{local: local}, nil 79 } 80 // Then, try to load it as a cloud plan. 81 cloud, cloudErr := cloudplan.LoadSavedPlanBookmark(filename) 82 if cloudErr == nil { 83 return &WrappedPlanFile{cloud: &cloud}, nil 84 } 85 // If neither worked, prioritize definitive "confirmed the format but can't 86 // use it" errors, then fall back to dumping everything we know. 87 var ulp *ErrUnusableLocalPlan 88 if errors.As(localErr, &ulp) { 89 return nil, ulp 90 } 91 92 combinedErr := fmt.Errorf("couldn't load the provided path as either a local plan file (%s) or a saved cloud plan (%s)", localErr, cloudErr) 93 return nil, combinedErr 94 }