-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Migrate Terraform commands from cli.Ui to views.View abstractions when rendering output. Investigate use of Streams, too. #37439
Description
Background
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.
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.
Getting inputs from users
Broadly this issue is specific to how we render outputs. Something probably beyond the scope of this issue and needs special consideration is how we collect input from users.
Input collection is done via the cli.Ui abstraction only. View is an alternative for how to handle output but there is no alternative for input. If we leave use of cli.Ui in the codebase for input we need to protect against it being used for output in new code after any migration to Views has occurred.
Problems
Views output warnings to stdout, whereas cli.Ui outputs warnings to stderr. If we don't handle this appropriately we're introducing breaking changes.
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