generated from terraform-linters/tflint-ruleset-template
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
59 lines (52 loc) · 1.32 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
package utils
import (
"fmt"
"os"
"regexp"
"strings"
"text/template"
)
var heading = regexp.MustCompile("(^[A-Za-z])|_([A-Za-z])")
// ToCamel converts a string to CamelCase
func ToCamel(str string) string {
exceptions := map[string]string{
"ip": "IP",
"sql": "SQL",
"vm": "VM",
"os": "OS",
"id": "ID",
"tls": "TLS",
}
parts := strings.Split(str, "_")
replaced := make([]string, len(parts))
for i, s := range parts {
conv, ok := exceptions[s]
if ok {
replaced[i] = conv
} else {
replaced[i] = s
}
}
str = strings.Join(replaced, "_")
return heading.ReplaceAllStringFunc(str, func(s string) string {
return strings.ToUpper(strings.Replace(s, "_", "", -1))
})
}
// GenerateFile generates a new file from the passed template and metadata
func GenerateFile(fileName string, tmplName string, meta interface{}) {
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
tmpl := template.Must(template.ParseFiles(tmplName))
err = tmpl.Execute(file, meta)
if err != nil {
panic(err)
}
}
// GenerateFileWithLogs generates a new file from the passed template and metadata
// The difference from GenerateFile function is to output logs
func GenerateFileWithLogs(fileName string, tmplName string, meta interface{}) {
GenerateFile(fileName, tmplName, meta)
fmt.Printf("Created: %s\n", fileName)
}