forked from Azure/terraform-azurerm-naming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (84 loc) · 2.3 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
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"regexp"
"sort"
"strings"
"text/template"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// Resource definition for the package
type Resource struct {
Name string `json:"name"`
Length *Length `json:"length,omitempty"`
Regex *string `json:"regex,omitempty"`
Scope *string `json:"scope,omitempty"`
Slug *string `json:"slug,omitempty"`
Dashes bool `json:"dashes"`
}
// Length allowed for that resorce
type Length struct {
Min int `json:"min"`
Max int `json:"max"`
}
func main() {
files, err := ioutil.ReadDir("templates")
if err != nil {
log.Fatal(err)
}
var fileNames = make([]string, len(files))
for i, file := range files {
fileNames[i] = "templates/" + file.Name()
}
caser := cases.Title(language.AmericanEnglish)
parsedTemplate, err := template.New("templates").Funcs(template.FuncMap{
// Terraform not yet support lookahead in their regex function
"cleanRegex": func(dirtyString string) string {
var re = regexp.MustCompile(`(?m)\(\?=.{\d+,\d+}\$\)|\(\?!\.\*--\)`)
return re.ReplaceAllString(dirtyString, "")
},
"replace": strings.ReplaceAll,
"title": caser.String,
}).ParseFiles(fileNames...)
if err != nil {
log.Fatal(err)
}
sourceDefinitions, err := ioutil.ReadFile("resourceDefinition.json")
if err != nil {
log.Fatal(err)
}
var data []Resource
err = json.Unmarshal(sourceDefinitions, &data)
if err != nil {
log.Fatal(err)
}
// Undocumented resource definitions
sourceDefinitionsUndocumented, err := ioutil.ReadFile("resourceDefinition_out_of_docs.json")
if err != nil {
log.Fatal(err)
}
var dataUndocumented []Resource
err = json.Unmarshal(sourceDefinitionsUndocumented, &dataUndocumented)
if err != nil {
log.Fatal(err)
}
data = append(data, dataUndocumented...)
// Sort the documented and undocumented resources alphabetically
sort.Slice(data, func(i, j int) bool {
return data[i].Name < data[j].Name
})
mainFile, err := os.OpenFile("main.tf", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
parsedTemplate.ExecuteTemplate(mainFile, "main", data)
outputsFile, err := os.OpenFile("outputs.tf", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
parsedTemplate.ExecuteTemplate(outputsFile, "outputs", data)
}