diff --git a/internal/command/meta_vars.go b/internal/command/meta_vars.go index 6f6c15dbb5..0da03614a6 100644 --- a/internal/command/meta_vars.go +++ b/internal/command/meta_vars.go @@ -85,117 +85,6 @@ func (m *Meta) collectVariableValuesForTests(testsFilePath string) (map[string]a return ret, diags } -// collectVariableValues inspects the various places that root module input variable -// values can come from and constructs a map ready to be passed to the -// backend as part of a backendrun.Operation. -// -// This method returns diagnostics relating to the collection of the values, -// but the values themselves may produce additional diagnostics when finally -// parsed. -func (m *Meta) collectVariableValues() (map[string]arguments.UnparsedVariableValue, tfdiags.Diagnostics) { - var diags tfdiags.Diagnostics - ret := map[string]arguments.UnparsedVariableValue{} - - // First we'll deal with environment variables, since they have the lowest - // precedence. - { - env := os.Environ() - for _, raw := range env { - if !strings.HasPrefix(raw, VarEnvPrefix) { - continue - } - raw = raw[len(VarEnvPrefix):] // trim the prefix - - eq := strings.Index(raw, "=") - if eq == -1 { - // Seems invalid, so we'll ignore it. - continue - } - - name := raw[:eq] - rawVal := raw[eq+1:] - - ret[name] = unparsedVariableValueString{ - str: rawVal, - name: name, - sourceType: terraform.ValueFromEnvVar, - } - } - } - - // Next up we have some implicit files that are loaded automatically - // if they are present. There's the original terraform.tfvars - // (DefaultVarsFilename) along with the later-added search for all files - // ending in .auto.tfvars. - if _, err := os.Stat(DefaultVarsFilename); err == nil { - moreDiags := m.addVarsFromFile(DefaultVarsFilename, terraform.ValueFromAutoFile, ret) - diags = diags.Append(moreDiags) - } - const defaultVarsFilenameJSON = DefaultVarsFilename + ".json" - if _, err := os.Stat(defaultVarsFilenameJSON); err == nil { - moreDiags := m.addVarsFromFile(defaultVarsFilenameJSON, terraform.ValueFromAutoFile, ret) - diags = diags.Append(moreDiags) - } - if infos, err := ioutil.ReadDir("."); err == nil { - // "infos" is already sorted by name, so we just need to filter it here. - for _, info := range infos { - name := info.Name() - if !isAutoVarFile(name) { - continue - } - moreDiags := m.addVarsFromFile(name, terraform.ValueFromAutoFile, ret) - diags = diags.Append(moreDiags) - } - } - - // Finally we process values given explicitly on the command line, either - // as individual literal settings or as additional files to read. - for _, flagNameValue := range m.variableArgs.AllItems() { - switch flagNameValue.Name { - case "-var": - // Value should be in the form "name=value", where value is a - // raw string whose interpretation will depend on the variable's - // parsing mode. - raw := flagNameValue.Value - eq := strings.Index(raw, "=") - if eq == -1 { - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Invalid -var option", - fmt.Sprintf("The given -var option %q is not correctly specified. Must be a variable name and value separated by an equals sign, like -var=\"key=value\".", raw), - )) - continue - } - name := raw[:eq] - rawVal := raw[eq+1:] - if strings.HasSuffix(name, " ") { - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Invalid -var option", - fmt.Sprintf("Variable name %q is invalid due to trailing space. Did you mean -var=\"%s=%s\"?", name, strings.TrimSuffix(name, " "), strings.TrimPrefix(rawVal, " ")), - )) - continue - } - ret[name] = unparsedVariableValueString{ - str: rawVal, - name: name, - sourceType: terraform.ValueFromCLIArg, - } - - case "-var-file": - moreDiags := m.addVarsFromFile(flagNameValue.Value, terraform.ValueFromNamedFile, ret) - diags = diags.Append(moreDiags) - - default: - // Should never happen; always a bug in the code that built up - // the contents of m.variableArgs. - diags = diags.Append(fmt.Errorf("unsupported variable option name %q (this is a bug in Terraform)", flagNameValue.Name)) - } - } - - return ret, diags -} - func (m *Meta) addVarsFromFile(filename string, sourceType terraform.ValueSourceType, to map[string]arguments.UnparsedVariableValue) tfdiags.Diagnostics { var diags tfdiags.Diagnostics