github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/views/plan.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package views 5 6 import ( 7 "fmt" 8 9 "github.com/terramate-io/tf/command/arguments" 10 "github.com/terramate-io/tf/terraform" 11 "github.com/terramate-io/tf/tfdiags" 12 ) 13 14 // The Plan view is used for the plan command. 15 type Plan interface { 16 Operation() Operation 17 Hooks() []terraform.Hook 18 19 Diagnostics(diags tfdiags.Diagnostics) 20 HelpPrompt() 21 } 22 23 // NewPlan returns an initialized Plan implementation for the given ViewType. 24 func NewPlan(vt arguments.ViewType, view *View) Plan { 25 switch vt { 26 case arguments.ViewJSON: 27 return &PlanJSON{ 28 view: NewJSONView(view), 29 } 30 case arguments.ViewHuman: 31 return &PlanHuman{ 32 view: view, 33 inAutomation: view.RunningInAutomation(), 34 } 35 default: 36 panic(fmt.Sprintf("unknown view type %v", vt)) 37 } 38 } 39 40 // The PlanHuman implementation renders human-readable text logs, suitable for 41 // a scrolling terminal. 42 type PlanHuman struct { 43 view *View 44 45 inAutomation bool 46 } 47 48 var _ Plan = (*PlanHuman)(nil) 49 50 func (v *PlanHuman) Operation() Operation { 51 return NewOperation(arguments.ViewHuman, v.inAutomation, v.view) 52 } 53 54 func (v *PlanHuman) Hooks() []terraform.Hook { 55 return []terraform.Hook{ 56 NewUiHook(v.view), 57 } 58 } 59 60 func (v *PlanHuman) Diagnostics(diags tfdiags.Diagnostics) { 61 v.view.Diagnostics(diags) 62 } 63 64 func (v *PlanHuman) HelpPrompt() { 65 v.view.HelpPrompt("plan") 66 } 67 68 // The PlanJSON implementation renders streaming JSON logs, suitable for 69 // integrating with other software. 70 type PlanJSON struct { 71 view *JSONView 72 } 73 74 var _ Plan = (*PlanJSON)(nil) 75 76 func (v *PlanJSON) Operation() Operation { 77 return &OperationJSON{view: v.view} 78 } 79 80 func (v *PlanJSON) Hooks() []terraform.Hook { 81 return []terraform.Hook{ 82 newJSONHook(v.view), 83 } 84 } 85 86 func (v *PlanJSON) Diagnostics(diags tfdiags.Diagnostics) { 87 v.view.Diagnostics(diags) 88 } 89 90 func (v *PlanJSON) HelpPrompt() { 91 }