Skip to content

Commit 33d7c1e

Browse files
committed
command/init: Add -cloud alias (of -backend)
This fixes an issue where a user could not disable initialization of the 'cloud' configuration block (As is possible with -backend=false), as well as add some syntactic sugar around -backend by adding a mutually exclusive -cloud alias.
1 parent d30314d commit 33d7c1e

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎internal/command/arguments/flags.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ type FlagNameValue struct {
8484
func (f FlagNameValue) String() string {
8585
return fmt.Sprintf("%s=%q", f.Name, f.Value)
8686
}
87+
88+
// FlagIsSet returns whether a flag is explicitly set in a set of flags
89+
func FlagIsSet(flags *flag.FlagSet, name string) bool {
90+
isSet := false
91+
flags.Visit(func(f *flag.Flag) {
92+
if f.Name == name {
93+
isSet = true
94+
}
95+
})
96+
return isSet
97+
}

‎internal/command/init.go‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hashicorp/terraform/internal/backend"
1717
backendInit "github.com/hashicorp/terraform/internal/backend/init"
1818
"github.com/hashicorp/terraform/internal/cloud"
19+
"github.com/hashicorp/terraform/internal/command/arguments"
1920
"github.com/hashicorp/terraform/internal/configs"
2021
"github.com/hashicorp/terraform/internal/configs/configschema"
2122
"github.com/hashicorp/terraform/internal/getproviders"
@@ -34,13 +35,14 @@ type InitCommand struct {
3435

3536
func (c *InitCommand) Run(args []string) int {
3637
var flagFromModule, flagLockfile string
37-
var flagBackend, flagGet, flagUpgrade bool
38+
var flagBackend, flagCloud, flagGet, flagUpgrade bool
3839
var flagPluginPath FlagStringSlice
3940
flagConfigExtra := newRawFlags("-backend-config")
4041

4142
args = c.Meta.process(args)
4243
cmdFlags := c.Meta.extendedFlagSet("init")
4344
cmdFlags.BoolVar(&flagBackend, "backend", true, "")
45+
cmdFlags.BoolVar(&flagCloud, "cloud", true, "")
4446
cmdFlags.Var(flagConfigExtra, "backend-config", "")
4547
cmdFlags.StringVar(&flagFromModule, "from-module", "", "copy the source of the given module into the directory before init")
4648
cmdFlags.BoolVar(&flagGet, "get", true, "")
@@ -58,6 +60,19 @@ func (c *InitCommand) Run(args []string) int {
5860
return 1
5961
}
6062

63+
backendFlagSet := arguments.FlagIsSet(cmdFlags, "backend")
64+
cloudFlagSet := arguments.FlagIsSet(cmdFlags, "cloud")
65+
66+
switch {
67+
case backendFlagSet && cloudFlagSet:
68+
c.Ui.Error("The -backend and -cloud options are aliases of one another and mutually-exclusive in their use")
69+
return 1
70+
case backendFlagSet:
71+
flagCloud = flagBackend
72+
case cloudFlagSet:
73+
flagBackend = flagCloud
74+
}
75+
6176
if c.migrateState && c.reconfigure {
6277
c.Ui.Error("The -migrate-state and -reconfigure options are mutually-exclusive")
6378
return 1
@@ -212,7 +227,7 @@ func (c *InitCommand) Run(args []string) int {
212227
var back backend.Backend
213228

214229
switch {
215-
case config.Module.CloudConfig != nil:
230+
case flagCloud && config.Module.CloudConfig != nil:
216231
be, backendOutput, backendDiags := c.initCloud(config.Module, flagConfigExtra)
217232
diags = diags.Append(backendDiags)
218233
if backendDiags.HasErrors() {
@@ -992,6 +1007,7 @@ func (c *InitCommand) AutocompleteArgs() complete.Predictor {
9921007
func (c *InitCommand) AutocompleteFlags() complete.Flags {
9931008
return complete.Flags{
9941009
"-backend": completePredictBoolean,
1010+
"-cloud": completePredictBoolean,
9951011
"-backend-config": complete.PredictFiles("*.tfvars"), // can also be key=value, but we can't "predict" that
9961012
"-force-copy": complete.PredictNothing,
9971013
"-from-module": completePredictModuleSource,
@@ -1026,8 +1042,11 @@ Usage: terraform [global options] init [options]
10261042
10271043
Options:
10281044
1029-
-backend=false Disable backend initialization for this configuration
1030-
and use the previously initialized backend instead.
1045+
-backend=false Disable backend or Terraform Cloud initialization for
1046+
this configuration and use what what was previously
1047+
initialized instead.
1048+
1049+
aliases: -cloud=false
10311050
10321051
-backend-config=path This can be either a path to an HCL file with key/value
10331052
assignments (same format as terraform.tfvars) or a

0 commit comments

Comments
 (0)