2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
import (
|
2021-05-17 12:11:06 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2021-02-22 10:55:00 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// InputEnabled is used to disable interactive input for unspecified
|
|
|
|
// variable and backend config values. Default is true.
|
|
|
|
InputEnabled bool
|
|
|
|
|
|
|
|
// OutPath contains an optional path to store the plan file
|
|
|
|
OutPath string
|
|
|
|
|
|
|
|
// ViewType specifies which output format to use
|
|
|
|
ViewType ViewType
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.BoolVar(&plan.InputEnabled, "input", true, "input")
|
|
|
|
cmdFlags.StringVar(&plan.OutPath, "out", "", "out")
|
|
|
|
|
2021-02-23 09:16:09 -06:00
|
|
|
var json bool
|
|
|
|
cmdFlags.BoolVar(&json, "json", false, "json")
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
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())
|
|
|
|
|
2021-02-23 09:16:09 -06:00
|
|
|
// JSON view currently does not support input, so we disable it here
|
|
|
|
if json {
|
|
|
|
plan.InputEnabled = false
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:55:00 -06:00
|
|
|
switch {
|
2021-02-23 09:16:09 -06:00
|
|
|
case json:
|
|
|
|
plan.ViewType = ViewJSON
|
2021-02-22 10:55:00 -06:00
|
|
|
default:
|
|
|
|
plan.ViewType = ViewHuman
|
|
|
|
}
|
|
|
|
|
|
|
|
return plan, diags
|
|
|
|
}
|