-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Description
Terraform Version
N/AUse Cases
Different parts of the code base render output using either the older cli.Ui abstraction, or the newer views.View abstraction.
Views were introduced in #27738 to minimise the code needed to render output in either Human or JSON formats. Since then, only commands that have the option to create JSON/machine-readable output have been migrated to using Views. Other commands are still using the old Ui approach, and it's possible for one command to use a mixture of the two, due to both being accessible in a command's Meta. Two parallel solutions for the same problem makes it harder to onboard to the codebase.
Attempted Solutions
N/A
Proposal
We should ensure that we migrate all commands over to using Views eventually. This appears to have been the plan in the past, but understandably it's a low-priority piece of work:
terraform/internal/command/meta.go
Lines 635 to 636 in eee744c
| // Reconfigure the view. This is necessary for commands which use both | |
| // views.View and cli.Ui during the migration phase. |
Commands that use the old UI approach still are:
- console
- fmt
- get
- graph
- import
- init
- login
- logout
- providers - all subcommands
- state - all subcommands
- taint
- unlock
- untaint
- version
- workspace - all subcommands
Also, an honourable mention for code in internal/command/meta_backend.go that mixes Views and Ui. That code is specific to the init command though, so could be addressed at the same time as the init command.
References
Code
The Meta allows access to both a Ui and a View:
terraform/internal/command/meta.go
Lines 75 to 79 in eee744c
| View *views.View | |
| Color bool // True if output should be colored | |
| GlobalPluginDirs []string // Additional paths to search for plugins | |
| Ui cli.Ui // Ui for output |
The Meta struct also has some methods for handling output via Ui:
terraform/internal/command/meta.go
Line 684 in eee744c
| func (m *Meta) showDiagnostics(vals ...interface{}) { |
The newer alternative(s) of the method above is the Diagnostics method in the Operation interface which is implemented differently for Human or JSON output:
terraform/internal/command/views/operation.go
Lines 24 to 37 in eee744c
| type Operation interface { | |
| Interrupted() | |
| FatalInterrupt() | |
| Stopping() | |
| Cancelled(planMode plans.Mode) | |
| EmergencyDumpState(stateFile *statefile.File) error | |
| PlannedChange(change *plans.ResourceInstanceChangeSrc) | |
| Plan(plan *plans.Plan, schemas *terraform.Schemas) | |
| PlanNextStep(planPath string, genConfigPath string) | |
| Diagnostics(diags tfdiags.Diagnostics) | |
| } |
Related issues
Potentially addresses #34768