cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 21:51:40 +01:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
import (
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 21:51:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Output represents the command-line arguments for the output command.
|
|
|
|
type Output struct {
|
|
|
|
// Name identifies which root module output to show. If empty, show all
|
|
|
|
// outputs.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// StatePath is an optional path to a state file, from which outputs will
|
|
|
|
// be loaded.
|
|
|
|
StatePath string
|
|
|
|
|
|
|
|
// ViewType specifies which output format to use: human, JSON, or "raw".
|
|
|
|
ViewType ViewType
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseOutput processes CLI arguments, returning an Output value and errors.
|
|
|
|
// If errors are encountered, an Output value is still returned representing
|
|
|
|
// the best effort interpretation of the arguments.
|
|
|
|
func ParseOutput(args []string) (*Output, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
output := &Output{}
|
|
|
|
|
|
|
|
var jsonOutput, rawOutput bool
|
|
|
|
var statePath string
|
|
|
|
cmdFlags := defaultFlagSet("output")
|
|
|
|
cmdFlags.BoolVar(&jsonOutput, "json", false, "json")
|
|
|
|
cmdFlags.BoolVar(&rawOutput, "raw", false, "raw")
|
|
|
|
cmdFlags.StringVar(&statePath, "state", "", "path")
|
|
|
|
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Failed to parse command-line flags",
|
|
|
|
err.Error(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) > 1 {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Unexpected argument",
|
|
|
|
"The output command expects exactly one argument with the name of an output variable or no arguments to show all outputs.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonOutput && rawOutput {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid output format",
|
|
|
|
"The -raw and -json options are mutually-exclusive.",
|
|
|
|
))
|
|
|
|
|
|
|
|
// Since the desired output format is unknowable, fall back to default
|
|
|
|
jsonOutput = false
|
|
|
|
rawOutput = false
|
|
|
|
}
|
|
|
|
|
|
|
|
output.StatePath = statePath
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
output.Name = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawOutput && output.Name == "" {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Output name required",
|
|
|
|
"You must give the name of a single output value when using the -raw option.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case jsonOutput:
|
|
|
|
output.ViewType = ViewJSON
|
|
|
|
case rawOutput:
|
|
|
|
output.ViewType = ViewRaw
|
|
|
|
default:
|
|
|
|
output.ViewType = ViewHuman
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, diags
|
|
|
|
}
|