-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.go
93 lines (77 loc) · 1.87 KB
/
generate.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
//go:build generate
// +build generate
//go:generate go run generate.go
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/pyneda/sukyan/db"
"gopkg.in/yaml.v3"
)
func toCamelCase(input string) string {
words := strings.Split(input, "_")
for i := range words {
words[i] = strings.Title(words[i])
}
return strings.Join(words, "")
}
type IssueTemplateWrapper struct {
Original db.IssueTemplate
CamelCaseCode string
}
func main() {
var issueTemplates []IssueTemplateWrapper
err := filepath.Walk("./db/kb", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".yaml") {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var issue db.IssueTemplate
if err := yaml.Unmarshal(data, &issue); err != nil {
return err
}
wrapper := IssueTemplateWrapper{
Original: issue,
CamelCaseCode: toCamelCase(string(issue.Code)),
}
issueTemplates = append(issueTemplates, wrapper)
}
return nil
})
if err != nil {
fmt.Println("Error walking the path:", err)
return
}
funcMap := template.FuncMap{
"backtick": func(s string) string {
// Replace any existing backticks in the string with '`' string literal
s = strings.ReplaceAll(s, "`", "`+\"`\"+`")
return "`" + s + "`"
},
}
tmpl, err := template.New("kb_template.go.tmpl").Funcs(funcMap).ParseFiles("db/kb/kb_template.go.tmpl")
if err != nil {
fmt.Println("Error parsing template:", err)
return
}
f, err := os.Create("db/kb_autogenerated.go")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer f.Close()
err = tmpl.Execute(f, issueTemplates)
if err != nil {
fmt.Println("Error executing template:", err)
return
}
fmt.Println("Successfully generated db/kb_autogenerated.go")
}