From 5d9b7cc0baca747d038cb266257d4e7516e61cc1 Mon Sep 17 00:00:00 2001 From: Marcelo Almeida <11047924+marceloalmeida@users.noreply.github.com> Date: Fri, 25 Dec 2020 05:48:21 +0000 Subject: [PATCH] Removed empty description from variable declaration (#11) * Removed empty description from variable declaration * Added command flag to place or not description on variables --- README.md | 24 ++++++++++++------------ cmd/cmd.go | 10 ++++++---- pkg/generator/terraform.go | 5 +++-- pkg/generator/vars.go | 17 +++++++++++------ 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 22e15fb..3edbb73 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/cmd/cmd.go b/cmd/cmd.go index 0578221..bf3ee1b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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 @@ -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") @@ -64,6 +66,6 @@ func runGenerator(cmd *cobra.Command, args []string) { return } - generator.Generate(tfFiles, varsFile, localsFile) + generator.Generate(tfFiles, varsFile, localsFile, varsDescription) } } diff --git a/pkg/generator/terraform.go b/pkg/generator/terraform.go index 1bfb1e1..950011e 100644 --- a/pkg/generator/terraform.go +++ b/pkg/generator/terraform.go @@ -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) { diff --git a/pkg/generator/vars.go b/pkg/generator/vars.go index 1c75063..30fc5a8 100644 --- a/pkg/generator/vars.go +++ b/pkg/generator/vars.go @@ -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 }} @@ -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) { @@ -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) }