forked from sonda2208/gpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
81 lines (66 loc) · 1.61 KB
/
gen.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
package main
import (
"html/template"
"log"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
ClientName string `envconfig:"OBJECT_NAME" required:"true"`
TemplateFilePath string `envconfig:"TEMPLATE_FILE" required:"true"`
RequiredFields string
SampleID string
SampleDataPath string
SampleListDataPath string
}
type GeneratorConfig struct {
ClientName string
CapitalizeClientName string
IsClass bool
RequiredFields []string
SampleID string
SampleData string
SampleListData string
}
func loadConfig(prefixEnv string) *Config {
conf := &Config{}
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
err = envconfig.Process(prefixEnv, conf)
if err != nil {
log.Fatal(err)
}
return conf
}
func generateCode(conf *GeneratorConfig, tmlpPath, outputFile string) error {
t, err := template.ParseFiles(tmlpPath)
if err != nil {
return err
}
output, err := os.Create(outputFile)
if err != nil {
return err
}
defer output.Close()
err = t.Execute(output, conf)
if err != nil {
return err
}
return nil
}
func main() {
conf := loadConfig("GEN")
genConf := &GeneratorConfig{}
genConf.ClientName = conf.ClientName
genConf.CapitalizeClientName = strings.Title(conf.ClientName)
genConf.IsClass = strings.HasSuffix(strings.ToLower(conf.ClientName), "class")
genConf.RequiredFields = strings.Split(conf.RequiredFields, ",")
err := generateCode(genConf, conf.TemplateFilePath, "./"+strings.ToLower(conf.ClientName)+".go")
if err != nil {
log.Fatal(err)
}
}