Skip to content

Commit

Permalink
feature: allow multiple --var-file to be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
msurovcak committed Dec 3, 2020
1 parent 70c3105 commit faccbf3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ variable "docker_ports" {
image_id = "xyz"
```

- Multiple files can be specified via providing more `--var-file` options, variables overrides as for `terraform` command.
```
$ cat my.tfvars
image_id = "xyz"
$ cat other.tfvars
image_id = "abc"
$ tfvar . --var-file my.tfvars --var-file other.tfvars
image_id = "abc"
```

For more info, checkout the `--help` page:

```
Expand All @@ -99,6 +111,7 @@ Flags:
variable definitions files e.g. terraform.tfvars[.json] *.auto.tfvars[.json]
-d, --debug Print debug log on stderr
-e, --env-var Print output in export TF_VAR_image_id=ami-abc123 format
--var-file Reads variable file, multiple files can be provided
-h, --help help for tfvar
--ignore-default Do not use defined default values
--version version for tfvar
Expand Down
8 changes: 4 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ variable definitions files e.g. terraform.tfvars[.json] *.auto.tfvars[.json]`)
rootCmd.PersistentFlags().Bool(flagNoDefault, false, "Do not use defined default values")
rootCmd.PersistentFlags().StringArray(flagVar, []string{}, `Set a variable in the generated definitions.
This flag can be set multiple times.`)
rootCmd.PersistentFlags().String(flagVarFile, "", `Set variables from a file.`)
rootCmd.PersistentFlags().StringArray(flagVarFile, []string{}, `Set variables from a file.`)

return rootCmd, func() {
if r.log != nil {
Expand Down Expand Up @@ -148,13 +148,13 @@ func (r *runner) rootRunE(cmd *cobra.Command, args []string) error {
}
}

fromFile, err := cmd.PersistentFlags().GetString(flagVarFile)
fromFiles, err := cmd.PersistentFlags().GetStringArray(flagVarFile)
if err != nil {
return errors.Wrap(err, "cmd: get flag --var-file")
}

if fromFile != "" {
if err := tfvar.CollectFromFile(fromFile, unparseds); err != nil {
for _, fv := range fromFiles {
if err := tfvar.CollectFromFile(fv, unparseds); err != nil {
return err
}
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ image_id = "xyz"
`, actual.String())
}

func TestMultipleVarFiles(t *testing.T) {
os.Args = strings.Fields("tfvar testdata --var-file testdata/my.tfvars --var-file testdata/other.tfvars")

var actual bytes.Buffer
cmd, sync := New(&actual, "dev")
defer sync()

require.NoError(t, cmd.Execute())
assert.Equal(t, `availability_zone_names = ["us-west-1a"]
docker_ports = [{
external = 8300
internal = 8300
protocol = "tcp"
}]
image_id = "abc"
`, actual.String())
}


func TestVarFileError(t *testing.T) {
os.Args = strings.Fields("tfvar testdata --var-file testdata/bad.tfvars")

Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/other.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
image_id = "abc"

0 comments on commit faccbf3

Please sign in to comment.