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:
|
// Reconfigure the view. This is necessary for commands which use both |
|
// views.View and cli.Ui during the migration phase. |
Not in scope
- Changes to how input is gathered.
- Eliminating use of github.com/hashicorp/cli
Progress
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.
Caveats
Both use of cli.Ui and views logs errors to stderr and warnings to stdout. See: #38811
References
Code
The Meta allows access to both a Ui and a View:
|
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:
|
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:
|
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
Background
Different parts of the code base render output using either the older
cli.Uiabstraction, or the newerviews.Viewabstraction.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
Uiapproach, 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
Not in scope
Progress
argumentspackage?viewspackage?Also, an honourable mention for code in
internal/command/meta_backend.gothat 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.Caveats
Both use of cli.Ui and views logs errors to stderr and warnings to stdout. See: #38811
References
Code
The
Metaallows access to both aUiand aView:terraform/internal/command/meta.go
Lines 75 to 79 in eee744c
The
Metastruct also has some methods for handling output viaUi:terraform/internal/command/meta.go
Line 684 in eee744c
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
Related issues
Potentially addresses #34768