Skip to content

Commit

Permalink
Add HCL variable support from varfiles
Browse files Browse the repository at this point in the history
Previously, all variables extracted from a file were considered strings.
This patch now detects lists as HCL variables and create them
accordingly.

Drive-by:
* Fixes the `getTag()` function in the Magefile.

Fixes #13
  • Loading branch information
rgreinho committed Mar 27, 2020
1 parent e2cd5fd commit 7a387f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

* Ability to detect HCL values from a varfile. [#14]

## [1.0.0] - 2020-03-25

Initial version with support for managing:
Expand All @@ -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
54 changes: 34 additions & 20 deletions cmd/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
}

Expand Down Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7a387f2

Please sign in to comment.