2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-07-18 13:37:27 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
core: Allow lists and maps as variable overrides
Terraform 0.7 introduces lists and maps as first-class values for
variables, in addition to string values which were previously available.
However, there was previously no way to override the default value of a
list or map, and the functionality for overriding specific map keys was
broken.
Using the environment variable method for setting variable values, there
was previously no way to give a variable a value of a list or map. These
now support HCL for individual values - specifying:
TF_VAR_test='["Hello", "World"]'
will set the variable `test` to a two-element list containing "Hello"
and "World". Specifying
TF_VAR_test_map='{"Hello = "World", "Foo" = "bar"}'
will set the variable `test_map` to a two-element map with keys "Hello"
and "Foo", and values "World" and "bar" respectively.
The same logic is applied to `-var` flags, and the file parsed by
`-var-files` ("autoVariables").
Note that care must be taken to not run into shell expansion for `-var-`
flags and environment variables.
We also merge map keys where appropriate. The override syntax has
changed (to be noted in CHANGELOG as a breaking change), so several
tests needed their syntax updating from the old `amis.us-east-1 =
"newValue"` style to `amis = "{ "us-east-1" = "newValue"}"` style as
defined in TF-002.
In order to continue supporting the `-var "foo=bar"` type of variable
flag (which is not valid HCL), a special case error is checked after HCL
parsing fails, and the old code path runs instead.
2016-07-20 20:38:26 -05:00
|
|
|
// FlagStringKV is a flag.Value implementation for parsing user variables
|
|
|
|
// from the command-line in the format of '-var key=value', where value is
|
|
|
|
// only ever a primitive.
|
|
|
|
type FlagStringKV map[string]string
|
|
|
|
|
|
|
|
func (v *FlagStringKV) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FlagStringKV) Set(raw string) error {
|
2014-07-18 13:37:27 -05:00
|
|
|
idx := strings.Index(raw, "=")
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf("No '=' value in arg: %s", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *v == nil {
|
|
|
|
*v = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value := raw[0:idx], raw[idx+1:]
|
|
|
|
(*v)[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-24 11:18:15 -05:00
|
|
|
// FlagStringSlice is a flag.Value implementation for parsing targets from the
|
|
|
|
// command line, e.g. -target=aws_instance.foo -target=aws_vpc.bar
|
|
|
|
type FlagStringSlice []string
|
|
|
|
|
|
|
|
func (v *FlagStringSlice) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
func (v *FlagStringSlice) Set(raw string) error {
|
|
|
|
*v = append(*v, raw)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|