Merge pull request #29987 from hashicorp/chrisarcand/backend-flag-with-tfc

command/init: Add -cloud alias to -backend, adjust `init` help output
This commit is contained in:
Chris Arcand 2021-11-29 08:06:08 -06:00 committed by GitHub
commit b5af7b6c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 15 deletions

View File

@ -84,3 +84,14 @@ type FlagNameValue struct {
func (f FlagNameValue) String() string {
return fmt.Sprintf("%s=%q", f.Name, f.Value)
}
// FlagIsSet returns whether a flag is explicitly set in a set of flags
func FlagIsSet(flags *flag.FlagSet, name string) bool {
isSet := false
flags.Visit(func(f *flag.Flag) {
if f.Name == name {
isSet = true
}
})
return isSet
}

View File

@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform/internal/backend"
backendInit "github.com/hashicorp/terraform/internal/backend/init"
"github.com/hashicorp/terraform/internal/cloud"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/getproviders"
@ -34,13 +35,14 @@ type InitCommand struct {
func (c *InitCommand) Run(args []string) int {
var flagFromModule, flagLockfile string
var flagBackend, flagGet, flagUpgrade bool
var flagBackend, flagCloud, flagGet, flagUpgrade bool
var flagPluginPath FlagStringSlice
flagConfigExtra := newRawFlags("-backend-config")
args = c.Meta.process(args)
cmdFlags := c.Meta.extendedFlagSet("init")
cmdFlags.BoolVar(&flagBackend, "backend", true, "")
cmdFlags.BoolVar(&flagCloud, "cloud", true, "")
cmdFlags.Var(flagConfigExtra, "backend-config", "")
cmdFlags.StringVar(&flagFromModule, "from-module", "", "copy the source of the given module into the directory before init")
cmdFlags.BoolVar(&flagGet, "get", true, "")
@ -58,6 +60,19 @@ func (c *InitCommand) Run(args []string) int {
return 1
}
backendFlagSet := arguments.FlagIsSet(cmdFlags, "backend")
cloudFlagSet := arguments.FlagIsSet(cmdFlags, "cloud")
switch {
case backendFlagSet && cloudFlagSet:
c.Ui.Error("The -backend and -cloud options are aliases of one another and mutually-exclusive in their use")
return 1
case backendFlagSet:
flagCloud = flagBackend
case cloudFlagSet:
flagBackend = flagCloud
}
if c.migrateState && c.reconfigure {
c.Ui.Error("The -migrate-state and -reconfigure options are mutually-exclusive")
return 1
@ -212,7 +227,7 @@ func (c *InitCommand) Run(args []string) int {
var back backend.Backend
switch {
case config.Module.CloudConfig != nil:
case flagCloud && config.Module.CloudConfig != nil:
be, backendOutput, backendDiags := c.initCloud(config.Module, flagConfigExtra)
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
@ -992,6 +1007,7 @@ func (c *InitCommand) AutocompleteArgs() complete.Predictor {
func (c *InitCommand) AutocompleteFlags() complete.Flags {
return complete.Flags{
"-backend": completePredictBoolean,
"-cloud": completePredictBoolean,
"-backend-config": complete.PredictFiles("*.tfvars"), // can also be key=value, but we can't "predict" that
"-force-copy": complete.PredictNothing,
"-from-module": completePredictModuleSource,
@ -1026,17 +1042,22 @@ Usage: terraform [global options] init [options]
Options:
-backend=false Disable backend initialization for this configuration
and use the previously initialized backend instead.
-backend=false Disable backend or Terraform Cloud initialization for
this configuration and use what what was previously
initialized instead.
-backend-config=path This can be either a path to an HCL file with key/value
aliases: -cloud=false
-backend-config=path Configuration to be merged with what is in the
configuration file's 'backend' block. This can be
either a path to an HCL file with key/value
assignments (same format as terraform.tfvars) or a
'key=value' format. This is merged with what is in the
configuration file. This can be specified multiple
'key=value' format, and can be specified multiple
times. The backend type must be in the configuration
itself.
-force-copy Suppress prompts about copying state data. This is
-force-copy Suppress prompts about copying state data when
initializating a new state backend. This is
equivalent to providing a "yes" to all confirmation
prompts.
@ -1045,9 +1066,9 @@ Options:
-get=false Disable downloading modules for this configuration.
-input=false Disable prompting for missing backend configuration
values. This will result in an error if the backend
configuration is not fully specified.
-input=false Disable interactive prompts. Note that some actions may
require interactive prompts and will error if input is
disabled.
-lock=false Don't hold a state lock during backend migration.
This is dangerous if others might concurrently run
@ -1062,10 +1083,10 @@ Options:
automatic installation of plugins. This flag can be used
multiple times.
-reconfigure Reconfigure the backend, ignoring any saved
-reconfigure Reconfigure a backend, ignoring any saved
configuration.
-migrate-state Reconfigure the backend, and attempt to migrate any
-migrate-state Reconfigure a backend, and attempt to migrate any
existing state.
-upgrade Install the latest module and provider versions
@ -1076,8 +1097,12 @@ Options:
-lockfile=MODE Set a dependency lockfile mode.
Currently only "readonly" is valid.
-ignore-remote-version A rare option used for the remote backend only. See
the remote backend documentation for more information.
-ignore-remote-version A rare option used for Terraform Cloud and the remote backend
only. Set this to ignore checking that the local and remote
Terraform versions use compatible state representations, making
an operation proceed even when there is a potential mismatch.
See the documentation on configuring Terraform with
Terraform Cloud for more information.
`
return strings.TrimSpace(helpText)