github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/views/json/change_summary.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package json 5 6 import "fmt" 7 8 type Operation string 9 10 const ( 11 OperationApplied Operation = "apply" 12 OperationDestroyed Operation = "destroy" 13 OperationPlanned Operation = "plan" 14 ) 15 16 type ChangeSummary struct { 17 Add int `json:"add"` 18 Change int `json:"change"` 19 Import int `json:"import"` 20 Remove int `json:"remove"` 21 Operation Operation `json:"operation"` 22 } 23 24 // The summary strings for apply and plan are accidentally a public interface 25 // used by Terraform Cloud and Terraform Enterprise, so the exact formats of 26 // these strings are important. 27 func (cs *ChangeSummary) String() string { 28 switch cs.Operation { 29 case OperationApplied: 30 if cs.Import > 0 { 31 return fmt.Sprintf("Apply complete! Resources: %d imported, %d added, %d changed, %d destroyed.", cs.Import, cs.Add, cs.Change, cs.Remove) 32 } 33 return fmt.Sprintf("Apply complete! Resources: %d added, %d changed, %d destroyed.", cs.Add, cs.Change, cs.Remove) 34 case OperationDestroyed: 35 return fmt.Sprintf("Destroy complete! Resources: %d destroyed.", cs.Remove) 36 case OperationPlanned: 37 if cs.Import > 0 { 38 return fmt.Sprintf("Plan: %d to import, %d to add, %d to change, %d to destroy.", cs.Import, cs.Add, cs.Change, cs.Remove) 39 } 40 return fmt.Sprintf("Plan: %d to add, %d to change, %d to destroy.", cs.Add, cs.Change, cs.Remove) 41 default: 42 return fmt.Sprintf("%s: %d add, %d change, %d destroy", cs.Operation, cs.Add, cs.Change, cs.Remove) 43 } 44 }