Skip to content

Commit c55d87b

Browse files
authored
Merge pull request #20 from shihanng/refactor
A bit of refactoring
2 parents 50fa461 + eb99f4c commit c55d87b

File tree

11 files changed

+374
-382
lines changed

11 files changed

+374
-382
lines changed

README.md

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,42 @@ variable "docker_ports" {
5252
docker_ports = null
5353
image_id = null
5454
```
55-
- **tfvar** also provides output in environment variable formats:
55+
- **tfvar** also provides other output formats:
56+
57+
- In environment variable formats with `-e` flag:
58+
5659
```
5760
$ tfvar . -e
5861
export TF_VAR_availability_zone_names='["us-west-1a"]'
5962
export TF_VAR_docker_ports='[{ external = 8300, internal = 8300, protocol = "tcp" }]'
6063
export TF_VAR_image_id=''
6164
```
65+
66+
- The `-r, --resource` flag outputs all variables as `tfe_variable`
67+
resource of [Terraform Enterprise (tfe) provider](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/variable).
68+
69+
- The `-w, --workspace` flag outputs all variables in the payload format for the
70+
[Workspace Variables API](https://www.terraform.io/docs/cloud/api/workspace-variables.html#sample-payload)
71+
<https://www.terraform.io/docs/cloud/api/workspace-variables.html#sample-payload>
72+
which can used together with `jq` to filter variables by key name.
73+
74+
```
75+
$ tfvar -w . | jq '. | select(.data.attributes.key == "region")'
76+
{
77+
"data": {
78+
"type": "vars",
79+
"attributes": {
80+
"key": "region",
81+
"value": "",
82+
"description": "",
83+
"category": "terraform",
84+
"hcl": false,
85+
"sensitive": false
86+
}
87+
}
88+
}
89+
```
90+
6291
- 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.
6392
```
6493
$ export TF_VAR_availability_zone_names='["custom_zone"]'
@@ -96,18 +125,6 @@ variable "docker_ports" {
96125
image_id = "abc"
97126
```
98127

99-
- 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>
100-
101-
- 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.
102-
```
103-
$ tfvar -w . | jq '. | select(.data.attributes.key == "region")'
104-
{
105-
"data": {
106-
...
107-
}
108-
}
109-
```
110-
111128
For more info, checkout the `--help` page:
112129

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

137154

cmd/cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ one would write it in variable definitions files (.tfvars).
5151
variable definitions files e.g. terraform.tfvars[.json] *.auto.tfvars[.json]`)
5252
rootCmd.PersistentFlags().BoolP(flagDebug, "d", false, "Print debug log on stderr")
5353
rootCmd.PersistentFlags().BoolP(flagEnvVar, "e", false, "Print output in export TF_VAR_image_id=ami-abc123 format")
54-
rootCmd.PersistentFlags().BoolP(flagResource, "r", false, "Print output in hashicorp/tfe tfe_variable resource format")
55-
rootCmd.PersistentFlags().BoolP(flagWorkspace, "w", false, "Print output variables as payloads for workspace API")
54+
rootCmd.PersistentFlags().BoolP(flagResource, "r", false, "Print output in Terraform Enterprise (tfe) provider's tfe_variable resource format")
55+
rootCmd.PersistentFlags().BoolP(flagWorkspace, "w", false, "Print output variables as payloads for Workspace Variables API")
5656
rootCmd.PersistentFlags().Bool(flagNoDefault, false, "Do not use defined default values")
5757
rootCmd.PersistentFlags().StringArray(flagVar, []string{}, `Set a variable in the generated definitions.
5858
This flag can be set multiple times.`)
@@ -193,7 +193,7 @@ func (r *runner) rootRunE(cmd *cobra.Command, args []string) error {
193193

194194
if isResource {
195195
r.log.Debug("Print outputs in tfe_resource format")
196-
writer = tfvar.WriteAsTFE_Resource
196+
writer = tfvar.WriteAsTFEResource
197197
}
198198
return writer(r.out, vars)
199199
}

cmd/cmd_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/sebdah/goldie/v2"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -44,6 +45,42 @@ export TF_VAR_password=''
4445
`, actual.String())
4546
}
4647

48+
func TestWFlag(t *testing.T) {
49+
os.Args = strings.Fields("tfvar testdata -w")
50+
51+
var actual bytes.Buffer
52+
cmd, sync := New(&actual, "dev")
53+
defer sync()
54+
55+
require.NoError(t, cmd.Execute())
56+
57+
g := goldie.New(
58+
t,
59+
goldie.WithNameSuffix(".golden.json"),
60+
goldie.WithDiffEngine(goldie.ColoredDiff),
61+
)
62+
63+
g.Assert(t, "w_flag", actual.Bytes())
64+
}
65+
66+
func TestRFlag(t *testing.T) {
67+
os.Args = strings.Fields("tfvar testdata -r")
68+
69+
var actual bytes.Buffer
70+
cmd, sync := New(&actual, "dev")
71+
defer sync()
72+
73+
require.NoError(t, cmd.Execute())
74+
75+
g := goldie.New(
76+
t,
77+
goldie.WithNameSuffix(".golden.tf"),
78+
goldie.WithDiffEngine(goldie.ColoredDiff),
79+
)
80+
81+
g.Assert(t, "r_flag", actual.Bytes())
82+
}
83+
4784
func TestIgnoreDefault(t *testing.T) {
4885
os.Args = strings.Fields("tfvar testdata --ignore-default")
4986

cmd/testdata/r_flag.golden.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
resource "tfe_variable" "availability_zone_names" {
3+
key = "availability_zone_names"
4+
value = ["us-west-1a"]
5+
sensitive = false
6+
description = ""
7+
workspace_id = null
8+
category = "terraform"
9+
}
10+
11+
resource "tfe_variable" "docker_ports" {
12+
key = "docker_ports"
13+
value = [{
14+
external = 8300
15+
internal = 8300
16+
protocol = "tcp"
17+
}]
18+
sensitive = false
19+
description = ""
20+
workspace_id = null
21+
category = "terraform"
22+
}
23+
24+
resource "tfe_variable" "image_id" {
25+
key = "image_id"
26+
value = null
27+
sensitive = false
28+
description = ""
29+
workspace_id = null
30+
category = "terraform"
31+
}
32+
33+
resource "tfe_variable" "password" {
34+
key = "password"
35+
value = null
36+
sensitive = true
37+
description = "the root password to use with the database"
38+
workspace_id = null
39+
category = "terraform"
40+
}

cmd/testdata/w_flag.golden.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"data": {
3+
"type": "vars",
4+
"attributes": {
5+
"key": "availability_zone_names",
6+
"value": "['us-west-1a']",
7+
"description": "",
8+
"category": "terraform",
9+
"hcl": false,
10+
"sensitive": false
11+
}
12+
}
13+
}
14+
{
15+
"data": {
16+
"type": "vars",
17+
"attributes": {
18+
"key": "docker_ports",
19+
"value": "[{ external = 8300, internal = 8300, protocol = 'tcp' }]",
20+
"description": "",
21+
"category": "terraform",
22+
"hcl": false,
23+
"sensitive": false
24+
}
25+
}
26+
}
27+
{
28+
"data": {
29+
"type": "vars",
30+
"attributes": {
31+
"key": "image_id",
32+
"value": "",
33+
"description": "",
34+
"category": "terraform",
35+
"hcl": false,
36+
"sensitive": false
37+
}
38+
}
39+
}
40+
{
41+
"data": {
42+
"type": "vars",
43+
"attributes": {
44+
"key": "password",
45+
"value": "",
46+
"description": "the root password to use with the database",
47+
"category": "terraform",
48+
"hcl": false,
49+
"sensitive": true
50+
}
51+
}
52+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ require (
2020
github.com/kr/pretty v0.2.1 // indirect
2121
github.com/kr/text v0.2.0 // indirect
2222
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
23+
github.com/sebdah/goldie/v2 v2.5.3
24+
github.com/sergi/go-diff v1.2.0 // indirect
2325
github.com/spf13/afero v1.5.1 // indirect
2426
github.com/spf13/cobra v1.0.0
2527
github.com/spf13/pflag v1.0.5 // indirect

0 commit comments

Comments
 (0)