Files
opentf/internal/command/arguments/plan.go
Christian Mesh b212767263 Better error handling for -json-into argument
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2025-12-17 13:20:38 -05:00

80 lines
2.3 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"
)
// Plan represents the command-line arguments for the plan command.
type Plan struct {
// State, Operation, and Vars are the common extended flags
State *State
Operation *Operation
Vars *Vars
// DetailedExitCode enables different exit codes for error, success with
// changes, and success with no changes.
DetailedExitCode bool
// OutPath contains an optional path to store the plan file
OutPath string
// GenerateConfigPath tells OpenTofu that config should be generated for
// unmatched import target paths and which path the generated file should
// be written to.
GenerateConfigPath string
// ViewOptions specifies which view options to use
ViewOptions ViewOptions
// ShowSensitive is used to display the value of variables marked as sensitive.
ShowSensitive bool
}
// ParsePlan processes CLI arguments, returning a Plan value and errors.
// If errors are encountered, a Plan value is still returned representing
// the best effort interpretation of the arguments.
func ParsePlan(args []string) (*Plan, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
plan := &Plan{
State: &State{},
Operation: &Operation{},
Vars: &Vars{},
}
cmdFlags := extendedFlagSet("plan", plan.State, plan.Operation, plan.Vars)
cmdFlags.BoolVar(&plan.DetailedExitCode, "detailed-exitcode", false, "detailed-exitcode")
cmdFlags.StringVar(&plan.OutPath, "out", "", "out")
cmdFlags.StringVar(&plan.GenerateConfigPath, "generate-config-out", "", "generate-config-out")
cmdFlags.BoolVar(&plan.ShowSensitive, "show-sensitive", false, "displays sensitive values")
plan.ViewOptions.AddFlags(cmdFlags, true)
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) > 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Too many command line arguments",
"To specify a working directory for the plan, use the global -chdir flag.",
))
}
diags = diags.Append(plan.Operation.Parse())
diags = diags.Append(plan.ViewOptions.Parse())
return plan, diags
}