-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (63 loc) · 1.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bytes"
"embed"
"flag"
"log"
"os"
"text/template"
)
//go:embed *.tmpl.hcl
var content embed.FS
func main() {
rulesPtr := flag.String("rules", "", "One of no value, 'aws', 'gcp', 'azure'.")
rulesTagPtr := flag.String("rules-tag", "", "Should match a specific Git tag for the repo.")
flag.Parse()
baseTmpl := baseTemplate() // lint:edge_case_be_quiet_linter
if *rulesPtr == "aws" {
overlayTemplate("_aws.tmpl.hcl", baseTmpl, "_aws.tflint.hcl.tmp", *rulesTagPtr)
} else if *rulesPtr == "gcp" {
overlayTemplate("_gcp.tmpl.hcl", baseTmpl, "_gcp.tflint.hcl.tmp", *rulesTagPtr)
} else if *rulesPtr == "azure" {
overlayTemplate("_azure.tmpl.hcl", baseTmpl, "_azure.tflint.hcl.tmp", *rulesTagPtr)
}
}
func templateFormatWrite(tmpl *template.Template, writeFileName string, params map[string]interface{}) {
// File permissions that are also friendly to Windows machines.
const filePerms = 0o666
var buf bytes.Buffer
if err := tmpl.Execute(&buf, params); err != nil {
log.Fatal(err)
}
err := os.WriteFile(writeFileName, buf.Bytes(), filePerms) // lint:allow_666
if err != nil {
log.Fatal(err)
}
}
// Base configuration for non-cloud Terraform projects (e.g., New Relic,
// PagerDuty, Cloudflare).
func baseTemplate() *template.Template {
baseHCL, err := content.ReadFile("_base.tmpl.hcl")
if err != nil {
log.Fatal(err)
}
baseTmpl, err := template.New("base").Parse(string(baseHCL))
if err != nil {
log.Fatal(err)
}
templateFormatWrite(baseTmpl, "base.tflint.hcl", map[string]interface{}{})
return baseTmpl
}
func overlayTemplate(templateFileName string, baseTemplate *template.Template, writeFileName, tag string) {
overlayHCL, err := content.ReadFile(templateFileName)
if err != nil {
log.Fatal(err)
}
overlayTmpl, err := template.Must(baseTemplate.Clone()).Parse(string(overlayHCL))
if err != nil {
log.Fatal(err)
}
templateFormatWrite(overlayTmpl, writeFileName, map[string]interface{}{
"tag": tag,
})
}