Skip to content

Commit 5d9b7cc

Browse files
Removed empty description from variable declaration (#11)
* Removed empty description from variable declaration * Added command flag to place or not description on variables
1 parent 5c1d0e5 commit 5d9b7cc

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ go build .
2424
./terraform-variables-generator
2525
```
2626

27-
It will find all *.tf files in current directory, and generate variables.tf file. If you already have this file, it will ask to override it.
27+
It will find all `*.tf` files in current directory, and generate variables.tf file. If you already have this file, it will ask to override it.
2828

2929
### Example
3030

31-
```text
31+
```hcl
3232
resource "aws_vpc" "vpc" {
3333
cidr_block = var.cidr
3434
enable_dns_hostnames = var.enable_dns_hostnames
@@ -48,33 +48,33 @@ resource "aws_internet_gateway" "vpc" {
4848
}
4949
```
5050

51-
Will generate
51+
Will generate
5252

53-
```text
54-
variable "ami" {
55-
description = ""
53+
```hcl
54+
variable "ami" {
55+
description = ""
5656
}
5757
5858
variable "instance_type" {
59-
description = ""
59+
description = ""
6060
}
6161
6262
variable "cidr" {
63-
description = ""
63+
description = ""
6464
}
6565
6666
variable "enable_dns_hostnames" {
67-
description = ""
67+
description = ""
6868
}
6969
7070
variable "enable_dns_support" {
71-
description = ""
71+
description = ""
7272
}
7373
7474
variable "name" {
75-
description = ""
75+
description = ""
7676
}
77-
```
77+
```
7878

7979
## Tests
8080

cmd/cmd.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ const (
1919
var (
2020
generatorVersion string
2121

22-
vars bool
23-
varsFile string
24-
localsFile string
22+
vars bool
23+
varsDescription bool
24+
varsFile string
25+
localsFile string
2526
)
2627

2728
// Execute will run main logic
@@ -37,6 +38,7 @@ func Execute(version string) {
3738
}
3839

3940
cmd.PersistentFlags().BoolVar(&vars, "vars", true, "generate variables")
41+
cmd.PersistentFlags().BoolVar(&varsDescription, "vars-description", true, "include description on variables")
4042
cmd.PersistentFlags().StringVar(&varsFile, "vars-file", "./variables.tf", "path to generated variables file")
4143
cmd.PersistentFlags().StringVar(&localsFile, "locals-file", "./locals.tf", "path to generated locals file")
4244

@@ -64,6 +66,6 @@ func runGenerator(cmd *cobra.Command, args []string) {
6466
return
6567
}
6668

67-
generator.Generate(tfFiles, varsFile, localsFile)
69+
generator.Generate(tfFiles, varsFile, localsFile, varsDescription)
6870
}
6971
}

pkg/generator/terraform.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
)
1010

1111
type terraformVars struct {
12-
Variables []string
13-
Locals []string
12+
Locals []string
13+
Variables []string
14+
VariablesDescription bool
1415
}
1516

1617
func (t *terraformVars) matchVarPref(row, varPrefix string) {

pkg/generator/vars.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ var replacer *strings.Replacer
1616
var varPrefix = "var."
1717
var localPrefix = "local."
1818

19-
var varTemplate = template.Must(template.New("var_file").Parse(`{{range .}}
20-
variable "{{ . }}" {
19+
var varTemplate = template.Must(template.New("var_file").Funcs(template.FuncMap{"sub": sub}).Parse(`{{- $length := len .Variables -}}
20+
{{- range $i, $v := .Variables -}}
21+
{{ if $.VariablesDescription }}variable "{{ $v }}" {
2122
description = ""
22-
}
23-
{{end}}`))
23+
}{{ else }}variable "{{ $v }}" {}{{ end }}
24+
{{- if lt $i (sub $length 1) }}{{ "\n\n" }}{{ end -}}
25+
{{ end -}}{{printf "\n"}}`))
26+
27+
func sub(a, b int) int { return a - b }
2428

2529
var localsTemplate = template.Must(template.New("locals_file").Parse(`locals { {{ range . }}
2630
{{ . }} ={{ end }}
@@ -44,11 +48,12 @@ func init() {
4448
}
4549

4650
// Generate will write inputs to file
47-
func Generate(tfFiles []string, varsDstFile string, localsDstFile string) {
51+
func Generate(tfFiles []string, varsDstFile string, localsDstFile string, varsDescription bool) {
4852
var wg sync.WaitGroup
4953
messages := make(chan string)
5054
wg.Add(len(tfFiles))
5155
t := &terraformVars{}
56+
t.VariablesDescription = varsDescription
5257

5358
for _, file := range tfFiles {
5459
go func(file string) {
@@ -75,7 +80,7 @@ func Generate(tfFiles []string, varsDstFile string, localsDstFile string) {
7580
log.Infof("Variables are generated to %q file", varsDstFile)
7681

7782
t.sort(t.Variables)
78-
err = varTemplate.Execute(f, t.Variables)
83+
err = varTemplate.Execute(f, t)
7984
utils.CheckError(err)
8085
}
8186

0 commit comments

Comments
 (0)