-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Expand file tree
/
Copy pathplan.go
More file actions
123 lines (102 loc) · 3.66 KB
/
plan.go
File metadata and controls
123 lines (102 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package arguments
import (
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/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
// 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
// GenerateConfigPath tells Terraform that config should be generated for
// unmatched import target paths and which path the generated file should
// be written to.
GenerateConfigPath string
// Light enables "light plan" mode, where Terraform skips reading remote
// state for resources that have not changed in the local configuration
// or local state. The user is telling Terraform to trust that nothing
// has been modified outside of the local configuration.
Light bool
// 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")
cmdFlags.StringVar(&plan.GenerateConfigPath, "generate-config-out", "", "generate-config-out")
var json bool
cmdFlags.BoolVar(&json, "json", false, "json")
if err := cmdFlags.Parse(args); err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to parse command-line flags",
err.Error(),
))
}
if plan.State.StatePath != "" {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Warning,
"Deprecated flag: -state",
`Use the "path" attribute within the "local" backend to specify a file for state storage`,
))
}
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())
// Validate -light flag compatibility
if plan.Light {
if plan.Operation.PlanMode == plans.RefreshOnlyMode {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Incompatible plan options",
"The -light and -refresh-only options are mutually exclusive. Light mode skips refreshing unchanged resources, while refresh-only mode requires refreshing all resources.",
))
}
if plan.Operation.PlanMode == plans.DestroyMode {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Incompatible plan options",
"The -light and -destroy options are mutually exclusive. A destroy plan requires reading the current state of all resources.",
))
}
}
// JSON view currently does not support input, so we disable it here
if json {
plan.InputEnabled = false
}
switch {
case json:
plan.ViewType = ViewJSON
default:
plan.ViewType = ViewHuman
}
return plan, diags
}