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