Files
opentf/internal/command/arguments/output.go
2026-04-28 13:17:48 +03:00

93 lines
2.6 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package arguments
import (
"github.com/opentofu/opentofu/internal/tfdiags"
)
// 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
// ShowSensitive is used to display the value of variables marked as sensitive.
ShowSensitive bool
// ViewOptions specifies which view options to use
ViewOptions ViewOptions
// Vars and State are the common extended flags
Vars *Vars
State *State
}
// ParseOutput processes CLI arguments, returning an Output value, a closer function, 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, func(), tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
output := &Output{
Vars: &Vars{},
State: &State{},
}
var rawOutput bool
cmdFlags := extendedFlagSet("output", nil, output.Vars)
cmdFlags.BoolVar(&rawOutput, "raw", false, "raw")
output.State.AddFlags(cmdFlags, false, true, false, false)
cmdFlags.BoolVar(&output.ShowSensitive, "show-sensitive", false, "displays sensitive values")
output.ViewOptions.AddFlags(cmdFlags, false)
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.",
))
}
closer, moreDiags := output.ViewOptions.Parse()
diags = diags.Append(moreDiags)
if rawOutput {
output.ViewOptions.ViewType = ViewRaw
if output.ViewOptions.jsonFlag {
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
output.ViewOptions.ViewType = ViewHuman
rawOutput = false
}
}
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.",
))
}
return output, closer, diags
}