Skip to content

Commit

Permalink
Removed empty description from variable declaration (#11)
Browse files Browse the repository at this point in the history
* Removed empty description from variable declaration
* Added command flag to place or not description on variables
  • Loading branch information
marceloalmeida authored Dec 25, 2020
1 parent 5c1d0e5 commit 5d9b7cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ go build .
./terraform-variables-generator
```

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.
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.

### Example

```text
```hcl
resource "aws_vpc" "vpc" {
cidr_block = var.cidr
enable_dns_hostnames = var.enable_dns_hostnames
Expand All @@ -48,33 +48,33 @@ resource "aws_internet_gateway" "vpc" {
}
```

Will generate
Will generate

```text
variable "ami" {
description = ""
```hcl
variable "ami" {
description = ""
}
variable "instance_type" {
description = ""
description = ""
}
variable "cidr" {
description = ""
description = ""
}
variable "enable_dns_hostnames" {
description = ""
description = ""
}
variable "enable_dns_support" {
description = ""
description = ""
}
variable "name" {
description = ""
description = ""
}
```
```

## Tests

Expand Down
10 changes: 6 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const (
var (
generatorVersion string

vars bool
varsFile string
localsFile string
vars bool
varsDescription bool
varsFile string
localsFile string
)

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

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

Expand Down Expand Up @@ -64,6 +66,6 @@ func runGenerator(cmd *cobra.Command, args []string) {
return
}

generator.Generate(tfFiles, varsFile, localsFile)
generator.Generate(tfFiles, varsFile, localsFile, varsDescription)
}
}
5 changes: 3 additions & 2 deletions pkg/generator/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type terraformVars struct {
Variables []string
Locals []string
Locals []string
Variables []string
VariablesDescription bool
}

func (t *terraformVars) matchVarPref(row, varPrefix string) {
Expand Down
17 changes: 11 additions & 6 deletions pkg/generator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ var replacer *strings.Replacer
var varPrefix = "var."
var localPrefix = "local."

var varTemplate = template.Must(template.New("var_file").Parse(`{{range .}}
variable "{{ . }}" {
var varTemplate = template.Must(template.New("var_file").Funcs(template.FuncMap{"sub": sub}).Parse(`{{- $length := len .Variables -}}
{{- range $i, $v := .Variables -}}
{{ if $.VariablesDescription }}variable "{{ $v }}" {
description = ""
}
{{end}}`))
}{{ else }}variable "{{ $v }}" {}{{ end }}
{{- if lt $i (sub $length 1) }}{{ "\n\n" }}{{ end -}}
{{ end -}}{{printf "\n"}}`))

func sub(a, b int) int { return a - b }

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

// Generate will write inputs to file
func Generate(tfFiles []string, varsDstFile string, localsDstFile string) {
func Generate(tfFiles []string, varsDstFile string, localsDstFile string, varsDescription bool) {
var wg sync.WaitGroup
messages := make(chan string)
wg.Add(len(tfFiles))
t := &terraformVars{}
t.VariablesDescription = varsDescription

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

t.sort(t.Variables)
err = varTemplate.Execute(f, t.Variables)
err = varTemplate.Execute(f, t)
utils.CheckError(err)
}

Expand Down

0 comments on commit 5d9b7cc

Please sign in to comment.