diff --git a/CHANGELOG.md b/CHANGELOG.md index 832de46..b196c80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +* Ability to detect HCL variables from a varfile. [#14] + ## [1.0.0] - 2020-03-25 Initial version with support for managing: @@ -18,3 +22,4 @@ Initial version with support for managing: [1.0.0]: https://github.com/rgreinho/tfe-cli/releases/tag/1.0.0 [//]: # (Issue/PR links) +[#14]: https://github.com/rgreinho/tfe-cli/pull/14 diff --git a/cmd/variable.go b/cmd/variable.go index c3ad6bb..07c3bb2 100644 --- a/cmd/variable.go +++ b/cmd/variable.go @@ -74,19 +74,32 @@ var variableCreateCmd = &cobra.Command{ } // Convert the content to `key=value` format. - varsFile := []string{} + regularVarFile := []string{} + HCLVarFile := []string{} s := reflect.ValueOf(v) if s.Kind() == reflect.Map { for _, key := range s.MapKeys() { strct := s.MapIndex(key) k := fmt.Sprintf("%s", key.Interface()) - v := fmt.Sprintf("%s", strct.Interface()) - varsFile = append(varsFile, fmt.Sprintf("%s=%s", k, v)) + value := reflect.ValueOf(strct.Interface()) + // If the type is Slice, we consider it HCL. + if value.Kind() == reflect.Slice { + // Use reflection to extract the values of the slice. + b := make([]string, value.Len()) + for i := 0; i < value.Len(); i++ { + b[i] = fmt.Sprintf("%s", value.Index(i)) + } + HCLVarFile = append(HCLVarFile, fmt.Sprintf("%s=[%s]", k, strings.Join(b, ", "))) + } else { + // Otherwise it is always a regular variable. + regularVarFile = append(regularVarFile, fmt.Sprintf("%s=%s", k, strct.Interface())) + } } } // Add it to the list of variables to create. - varOptions = append(varOptions, createVariableOptions(varsFile, tfe.CategoryTerraform, false, false)...) + varOptions = append(varOptions, createVariableOptions(regularVarFile, tfe.CategoryTerraform, false, false)...) + varOptions = append(varOptions, createVariableOptions(HCLVarFile, tfe.CategoryTerraform, true, false)...) } // List existing variables. @@ -106,23 +119,24 @@ var variableCreateCmd = &cobra.Command{ //Check if the variable already exists. v, exists := indexedVars[*(opts.Key)] - // Update an existing variable. - if exists && force { - options := tfe.VariableUpdateOptions{ - Key: opts.Key, - Value: opts.Value, - HCL: opts.HCL, - Sensitive: opts.Sensitive, - } - if _, err := client.Variables.Update(context.Background(), workspace.ID, v.ID, options); err != nil { - log.Fatalf("Cannot update variable %q: %s.", *(opts.Key), err) + // If the variable exists. + if exists { + // Update it. + if force { + options := tfe.VariableUpdateOptions{ + Key: opts.Key, + Value: opts.Value, + HCL: opts.HCL, + Sensitive: opts.Sensitive, + } + if _, err := client.Variables.Update(context.Background(), workspace.ID, v.ID, options); err != nil { + log.Fatalf("Cannot update variable %q: %s.", *(opts.Key), err) + } + log.Debugf("Variable %q updated successfully.", *(opts.Key)) + continue } - log.Debugf("Variable %q updated successfully.", *(opts.Key)) - continue - } - // Skip an existing variable. - if exists { + // Raise an error.. log.Fatalf("Cannot create %q: variable already exists.", *(opts.Key)) } @@ -181,7 +195,7 @@ func init() { variableCreateCmd.Flags().StringArray("shvar", []string{}, "Create a sensitive HCL variable") variableCreateCmd.Flags().StringArray("evar", []string{}, "Create an environment variable") variableCreateCmd.Flags().StringArray("sevar", []string{}, "Create a sensitive environment variable") - variableCreateCmd.Flags().String("var-file", "", "Create HCL non-sensitive variables from a file") + variableCreateCmd.Flags().String("var-file", "", "Create non-sensitive regular and HCL variables from a file") variableCreateCmd.Flags().BoolP("force", "f", false, "Overwrite a variable if it exists") } diff --git a/magefile.go b/magefile.go index bf836ce..a44ff52 100644 --- a/magefile.go +++ b/magefile.go @@ -134,12 +134,12 @@ func getTag() string { // If the tag could not be retrieved from the environment, use Git. if tag == "" { - tag, err := sh.Output("git", "describe") + t, err := sh.Output("git", "describe") if err != nil { fmt.Printf("Cannot retrieve current git tag: %s.\n", err) os.Exit(1) } - tag = tag + tag = t } return tag