Skip to content

Commit

Permalink
Merge pull request #3 from AlexRudd/master
Browse files Browse the repository at this point in the history
Match all valid variables; sort variable; tabs over spaces
  • Loading branch information
alexandrst88 committed May 24, 2018
2 parents 1c73152 + ee16674 commit 680b1c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
11 changes: 10 additions & 1 deletion configuration.mock
Expand Up @@ -8,4 +8,13 @@ resource "aws_nat_gateway" "nat" {
allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
count = "${length(var.public_subnets)}"
}
}

data "template_file" "template1" {
template = "${file("${path.module}/template1.tpl")}"
vars {
t1_var1 = "${var.t1_var1}"
t1-var2 = "${var.t1-var2}"
t1-var3 = "${var.t1-Var3}-${var.t1-inline}"
}
}
6 changes: 3 additions & 3 deletions generator.go
Expand Up @@ -17,10 +17,9 @@ var tfFileExt = "*.tf"
var dstFile = "./variables.tf"
var varTemplate = template.Must(template.New("var_file").Parse(`{{range .}}
variable "{{ . }}" {
description = ""
description = ""
}
{{end}}
`))
{{end}}`))

func init() {
replacer = strings.NewReplacer(":", ".",
Expand Down Expand Up @@ -72,6 +71,7 @@ func main() {
f, err := os.Create(dstFile)
checkError(err)

t.sortVars()
err = varTemplate.Execute(f, t.Variables)
checkError(err)
log.Infof("Variables are generated to %q file", dstFile)
Expand Down
5 changes: 3 additions & 2 deletions generator_test.go
Expand Up @@ -41,8 +41,9 @@ func TestMatchVariable(t *testing.T) {
for _, text := range messages {
ter.matchVarPref(text, varPrefix)
}
if len(ter.Variables) != 1 {
t.Errorf("Should return one variable. but returned %d", len(ter.Variables))
if len(ter.Variables) != 5 {
t.Errorf("Should return five variable. but returned %d", len(ter.Variables))
t.Errorf("Variables found: %s", ter.Variables)
}

}
13 changes: 9 additions & 4 deletions terraform.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"regexp"
"sort"
"strings"
)

Expand All @@ -11,13 +12,17 @@ type terraformVars struct {

func (t *terraformVars) matchVarPref(row, varPrefix string) {
if strings.Contains(row, varPrefix) {
pattern := regexp.MustCompile(`var.([a-z?_]+)`)
match := pattern.FindAllStringSubmatch(row, 1)
if len(match) != 0 {
res := replacer.Replace(match[0][0])
pattern := regexp.MustCompile(`var.([a-z?A-Z?0-9?_][a-z?A-Z?0-9?_?-]*)`)
match := pattern.FindAllStringSubmatch(row, -1)
for _, m := range match {
res := replacer.Replace(m[0])
if !containsElement(t.Variables, res) {
t.Variables = append(t.Variables, res)
}
}
}
}

func (t *terraformVars) sortVars() {
sort.Sort(sort.StringSlice(t.Variables))
}

0 comments on commit 680b1c3

Please sign in to comment.