-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
77 lines (68 loc) · 1.25 KB
/
utils.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
package entber
import (
"bytes"
"log"
"os"
"path"
"strings"
"text/template"
"entgo.io/ent/entc/gen"
)
func parseTemplate(name string, data any) string {
in, err := templates.ReadFile("templates/" + name + ".tmpl")
if err != nil {
log.Fatalln(err)
}
t, err := template.New(name).Funcs(gen.Funcs).Parse(string(in))
if err != nil {
log.Fatalln(err)
}
out := new(bytes.Buffer)
err = t.Execute(out, data)
if err != nil {
log.Fatalln(err)
}
return out.String()
}
func in[T comparable](v T, vs []T) bool {
for _, v2 := range vs {
if v == v2 {
return true
}
}
return false
}
func has_prefixes(s string, px []string) bool {
for _, p := range px {
if strings.HasPrefix(s, p) {
return true
}
}
return false
}
func writeFile(destination string, content string) {
os.MkdirAll(path.Dir(destination), 0777)
err := os.WriteFile(destination, []byte(content), 0777)
if err != nil {
log.Fatalln(err)
}
}
func catch(err error) {
if err != nil {
log.Fatalln(err)
}
}
func rootDir() string {
current, err := os.Getwd()
catch(err)
again:
_, err = os.Stat(path.Join(current, "go.mod"))
if err != nil {
current = path.Join(current, "../")
if current == "/" {
log.Fatalln("go.mod not found")
}
goto again
}
return current
}