terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Migrate Terraform commands from `cli.Ui` to `views.View` abstractions when rendering output. Investigate use of `Streams`, too.

Open SarahFrench opened this issue 4 months ago • 5 comments

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 https://github.com/hashicorp/terraform/pull/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:

https://github.com/hashicorp/terraform/blob/eee744c8874f15c131651d8b34bd4860fdebcaed/internal/command/meta.go#L635-L636

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:

https://github.com/hashicorp/terraform/blob/eee744c8874f15c131651d8b34bd4860fdebcaed/internal/command/meta.go#L75-L79

The Meta struct also has some methods for handling output via Ui:

https://github.com/hashicorp/terraform/blob/eee744c8874f15c131651d8b34bd4860fdebcaed/internal/command/meta.go#L684

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:

https://github.com/hashicorp/terraform/blob/eee744c8874f15c131651d8b34bd4860fdebcaed/internal/command/views/operation.go#L24-L37

Related issues

Potentially addresses https://github.com/hashicorp/terraform/issues/34768

SarahFrench avatar Aug 13 '25 14:08 SarahFrench

A consideration with this migration is whether HCP Terraform parses 'human' output from the unmigrated commands currently, and how confident we are to consider the output via Ui and output through a View in a OperationHuman equivalent. We probably want to work with the Core Cloud team to ensure this issue's changes doesn't cause any integration issues, if we do this work.

SarahFrench avatar Aug 13 '25 14:08 SarahFrench

Hi! I’d like to work on this issue. Could you please share some pointers or guidance on how to get started? Thanks!

SrushtiSapkale avatar Aug 23 '25 10:08 SrushtiSapkale

@SrushtiSapkale Please see https://github.com/hashicorp/terraform/blob/main/.github/CONTRIBUTING.md.

crw avatar Aug 25 '25 18:08 crw

For anyone interested in this issue, please note that it will require the reviewer to work with other teams internal to HashiCorp before merging the changes.

A consideration with this migration is whether HCP Terraform parses 'human' output from the unmigrated commands currently, and how confident we are to consider the output via Ui and output through a View in a OperationHuman equivalent. We probably want to work with the Core Cloud team to ensure this issue's changes doesn't cause any integration issues, if we do this work.

SarahFrench avatar Aug 26 '25 13:08 SarahFrench

Another thing I've found: Some commands may use Streams to render output, such as the state show command. It's also used in test, graph and others.

It's unclear what the pros and cons are between Streams and Views, and why one may be used over the other.

https://github.com/hashicorp/terraform/blob/808713f56f5170df3950e9d043a0860504495611/internal/cloud/test.go#L98-L101

SarahFrench avatar Nov 21 '25 17:11 SarahFrench