Skip to content

Migrate Terraform commands from cli.Ui to views.View abstractions when rendering output #37439

@SarahFrench

Description

@SarahFrench

Terraform Version

N/A

Use 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:

// 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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions