Skip to content

Commit 680b1c3

Browse files
authored
Merge pull request #3 from AlexRudd/master
Match all valid variables; sort variable; tabs over spaces
2 parents 1c73152 + ee16674 commit 680b1c3

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

configuration.mock

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ resource "aws_nat_gateway" "nat" {
88
allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
99
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
1010
count = "${length(var.public_subnets)}"
11-
}
11+
}
12+
13+
data "template_file" "template1" {
14+
template = "${file("${path.module}/template1.tpl")}"
15+
vars {
16+
t1_var1 = "${var.t1_var1}"
17+
t1-var2 = "${var.t1-var2}"
18+
t1-var3 = "${var.t1-Var3}-${var.t1-inline}"
19+
}
20+
}

generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ var tfFileExt = "*.tf"
1717
var dstFile = "./variables.tf"
1818
var varTemplate = template.Must(template.New("var_file").Parse(`{{range .}}
1919
variable "{{ . }}" {
20-
description = ""
20+
description = ""
2121
}
22-
{{end}}
23-
`))
22+
{{end}}`))
2423

2524
func init() {
2625
replacer = strings.NewReplacer(":", ".",
@@ -72,6 +71,7 @@ func main() {
7271
f, err := os.Create(dstFile)
7372
checkError(err)
7473

74+
t.sortVars()
7575
err = varTemplate.Execute(f, t.Variables)
7676
checkError(err)
7777
log.Infof("Variables are generated to %q file", dstFile)

generator_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ func TestMatchVariable(t *testing.T) {
4141
for _, text := range messages {
4242
ter.matchVarPref(text, varPrefix)
4343
}
44-
if len(ter.Variables) != 1 {
45-
t.Errorf("Should return one variable. but returned %d", len(ter.Variables))
44+
if len(ter.Variables) != 5 {
45+
t.Errorf("Should return five variable. but returned %d", len(ter.Variables))
46+
t.Errorf("Variables found: %s", ter.Variables)
4647
}
4748

4849
}

terraform.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"regexp"
5+
"sort"
56
"strings"
67
)
78

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

1213
func (t *terraformVars) matchVarPref(row, varPrefix string) {
1314
if strings.Contains(row, varPrefix) {
14-
pattern := regexp.MustCompile(`var.([a-z?_]+)`)
15-
match := pattern.FindAllStringSubmatch(row, 1)
16-
if len(match) != 0 {
17-
res := replacer.Replace(match[0][0])
15+
pattern := regexp.MustCompile(`var.([a-z?A-Z?0-9?_][a-z?A-Z?0-9?_?-]*)`)
16+
match := pattern.FindAllStringSubmatch(row, -1)
17+
for _, m := range match {
18+
res := replacer.Replace(m[0])
1819
if !containsElement(t.Variables, res) {
1920
t.Variables = append(t.Variables, res)
2021
}
2122
}
2223
}
2324
}
25+
26+
func (t *terraformVars) sortVars() {
27+
sort.Sort(sort.StringSlice(t.Variables))
28+
}

0 commit comments

Comments
 (0)