Skip to content

Commit

Permalink
Merge pull request #20 from shihanng/refactor
Browse files Browse the repository at this point in the history
A bit of refactoring
  • Loading branch information
shihanng authored Nov 26, 2021
2 parents 50fa461 + eb99f4c commit c55d87b
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 382 deletions.
47 changes: 32 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,42 @@ variable "docker_ports" {
docker_ports = null
image_id = null
```
- **tfvar** also provides output in environment variable formats:
- **tfvar** also provides other output formats:
- In environment variable formats with `-e` flag:
```
$ tfvar . -e
export TF_VAR_availability_zone_names='["us-west-1a"]'
export TF_VAR_docker_ports='[{ external = 8300, internal = 8300, protocol = "tcp" }]'
export TF_VAR_image_id=''
```
- The `-r, --resource` flag outputs all variables as `tfe_variable`
resource of [Terraform Enterprise (tfe) provider](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/variable).
- The `-w, --workspace` flag outputs all variables in the payload format for the
[Workspace Variables API](https://www.terraform.io/docs/cloud/api/workspace-variables.html#sample-payload)
<https://www.terraform.io/docs/cloud/api/workspace-variables.html#sample-payload>
which can used together with `jq` to filter variables by key name.
```
$ tfvar -w . | jq '. | select(.data.attributes.key == "region")'
{
"data": {
"type": "vars",
"attributes": {
"key": "region",
"value": "",
"description": "",
"category": "terraform",
"hcl": false,
"sensitive": false
}
}
}
```
- There is also `--auto-assign` option for those who wants the values from `terraform.tfvars[.json]`, `*.auto.tfvars[.json]`, and environment variables (`TF_VAR_` followed by the name of a declared variable) to be assigned to the generated definitions automatically.
```
$ export TF_VAR_availability_zone_names='["custom_zone"]'
Expand Down Expand Up @@ -96,18 +125,6 @@ variable "docker_ports" {
image_id = "abc"
```

- The `-r, --resource` flag outputs all variables as terraform resources for the `tfe_variable resource` found in the `tfe` provider <https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/variable>

- The `-w, --workspace` flag outputs all variables in the payload format for the API <https://www.terraform.io/docs/cloud/api/workspace-variables.html#sample-payload>. You can use `jq` to filter variables by key name.
```
$ tfvar -w . | jq '. | select(.data.attributes.key == "region")'
{
"data": {
...
}
}
```
For more info, checkout the `--help` page:

```
Expand All @@ -125,13 +142,13 @@ Flags:
-e, --env-var Print output in export TF_VAR_image_id=ami-abc123 format
-h, --help help for tfvar
--ignore-default Do not use defined default values
-r, --resource Print output in hashicorp/tfe tfe_variable resource format
-r, --resource Print output in Terraform Enterprise (tfe) provider's tfe_variable resource format
--var stringArray Set a variable in the generated definitions.
This flag can be set multiple times.
--var-file stringArray Set variables from a file.
This flag can be set multiple times.
-v, --version version for tfvar
-w, --workspace Print output variables as payloads for workspace API
-w, --workspace Print output variables as payloads for Workspace Variables API
```


Expand Down
6 changes: 3 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ one would write it in variable definitions files (.tfvars).
variable definitions files e.g. terraform.tfvars[.json] *.auto.tfvars[.json]`)
rootCmd.PersistentFlags().BoolP(flagDebug, "d", false, "Print debug log on stderr")
rootCmd.PersistentFlags().BoolP(flagEnvVar, "e", false, "Print output in export TF_VAR_image_id=ami-abc123 format")
rootCmd.PersistentFlags().BoolP(flagResource, "r", false, "Print output in hashicorp/tfe tfe_variable resource format")
rootCmd.PersistentFlags().BoolP(flagWorkspace, "w", false, "Print output variables as payloads for workspace API")
rootCmd.PersistentFlags().BoolP(flagResource, "r", false, "Print output in Terraform Enterprise (tfe) provider's tfe_variable resource format")
rootCmd.PersistentFlags().BoolP(flagWorkspace, "w", false, "Print output variables as payloads for Workspace Variables API")
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.`)
Expand Down Expand Up @@ -193,7 +193,7 @@ func (r *runner) rootRunE(cmd *cobra.Command, args []string) error {

if isResource {
r.log.Debug("Print outputs in tfe_resource format")
writer = tfvar.WriteAsTFE_Resource
writer = tfvar.WriteAsTFEResource
}
return writer(r.out, vars)
}
37 changes: 37 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -44,6 +45,42 @@ export TF_VAR_password=''
`, actual.String())
}

func TestWFlag(t *testing.T) {
os.Args = strings.Fields("tfvar testdata -w")

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

require.NoError(t, cmd.Execute())

g := goldie.New(
t,
goldie.WithNameSuffix(".golden.json"),
goldie.WithDiffEngine(goldie.ColoredDiff),
)

g.Assert(t, "w_flag", actual.Bytes())
}

func TestRFlag(t *testing.T) {
os.Args = strings.Fields("tfvar testdata -r")

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

require.NoError(t, cmd.Execute())

g := goldie.New(
t,
goldie.WithNameSuffix(".golden.tf"),
goldie.WithDiffEngine(goldie.ColoredDiff),
)

g.Assert(t, "r_flag", actual.Bytes())
}

func TestIgnoreDefault(t *testing.T) {
os.Args = strings.Fields("tfvar testdata --ignore-default")

Expand Down
40 changes: 40 additions & 0 deletions cmd/testdata/r_flag.golden.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

resource "tfe_variable" "availability_zone_names" {
key = "availability_zone_names"
value = ["us-west-1a"]
sensitive = false
description = ""
workspace_id = null
category = "terraform"
}

resource "tfe_variable" "docker_ports" {
key = "docker_ports"
value = [{
external = 8300
internal = 8300
protocol = "tcp"
}]
sensitive = false
description = ""
workspace_id = null
category = "terraform"
}

resource "tfe_variable" "image_id" {
key = "image_id"
value = null
sensitive = false
description = ""
workspace_id = null
category = "terraform"
}

resource "tfe_variable" "password" {
key = "password"
value = null
sensitive = true
description = "the root password to use with the database"
workspace_id = null
category = "terraform"
}
52 changes: 52 additions & 0 deletions cmd/testdata/w_flag.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"data": {
"type": "vars",
"attributes": {
"key": "availability_zone_names",
"value": "['us-west-1a']",
"description": "",
"category": "terraform",
"hcl": false,
"sensitive": false
}
}
}
{
"data": {
"type": "vars",
"attributes": {
"key": "docker_ports",
"value": "[{ external = 8300, internal = 8300, protocol = 'tcp' }]",
"description": "",
"category": "terraform",
"hcl": false,
"sensitive": false
}
}
}
{
"data": {
"type": "vars",
"attributes": {
"key": "image_id",
"value": "",
"description": "",
"category": "terraform",
"hcl": false,
"sensitive": false
}
}
}
{
"data": {
"type": "vars",
"attributes": {
"key": "password",
"value": "",
"description": "the root password to use with the database",
"category": "terraform",
"hcl": false,
"sensitive": true
}
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require (
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/sebdah/goldie/v2 v2.5.3
github.com/sergi/go-diff v1.2.0 // indirect
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
Loading

0 comments on commit c55d87b

Please sign in to comment.