-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
121 lines (96 loc) · 2.63 KB
/
config.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package utils
import (
"flag"
"os"
"github.com/BurntSushi/toml"
"golang.org/x/mod/modfile"
)
var (
configFile string
)
func init() {
flag.StringVar(&configFile, "config", "sqlboiler.toml", "Filename of config file to override default lookup")
flag.StringVar(&configFile, "c", "sqlboiler.toml", "Filename of config file to override default lookup")
flag.Parse()
}
type Config struct {
sqlBoilerConfig *SQLBoilerConfig
modFile *modfile.File
}
func ParseConfig() (*Config, error) {
sqlBoilerConfig, err := parseSQLBoilerConfig()
if err != nil {
return nil, err
}
modFile, err := parseModFile()
if err != nil {
return nil, err
}
return &Config{
sqlBoilerConfig: sqlBoilerConfig,
modFile: modFile,
}, nil
}
type ERGConfig struct {
Output string `toml:"output"`
OutputTS string `toml:"output-ts"`
Pkgname string `toml:"pkgname"`
Wipe bool `toml:"wipe"`
Inline bool `toml:"inline"`
Blacklist []string `toml:"blacklist"`
}
type TagCase string
const (
TagCaseCamel TagCase = "camel"
TagCaseSnake TagCase = "snake"
TagCaseTitle TagCase = "title"
TagCaseAlias TagCase = "alias"
)
type StructTagCases struct {
Json TagCase `toml:"json,omitempty" json:"json,omitempty"`
Yaml TagCase `toml:"yaml,omitempty" json:"yaml,omitempty"`
Toml TagCase `toml:"toml,omitempty" json:"toml,omitempty"`
Boil TagCase `toml:"boil,omitempty" json:"boil,omitempty"`
}
type SQLBoilerConfig struct {
Output string `toml:"output"`
PkgName string `toml:"pkgname"`
StructTagCases StructTagCases `toml:"struct-tag-cases"`
Erg ERGConfig `toml:"erg"`
}
func parseSQLBoilerConfig() (*SQLBoilerConfig, error) {
var sqlBoilerConfig SQLBoilerConfig
if _, err := toml.DecodeFile(configFile, &sqlBoilerConfig); err != nil {
return nil, err
}
if sqlBoilerConfig.StructTagCases.Boil == "" {
sqlBoilerConfig.StructTagCases.Boil = TagCaseSnake
}
if sqlBoilerConfig.StructTagCases.Json == "" {
sqlBoilerConfig.StructTagCases.Json = TagCaseSnake
}
if sqlBoilerConfig.StructTagCases.Yaml == "" {
sqlBoilerConfig.StructTagCases.Yaml = TagCaseSnake
}
if sqlBoilerConfig.StructTagCases.Toml == "" {
sqlBoilerConfig.StructTagCases.Toml = TagCaseSnake
}
if sqlBoilerConfig.Erg.Output == "" {
sqlBoilerConfig.Erg.Output = "erg_models"
}
if sqlBoilerConfig.Erg.Pkgname == "" {
sqlBoilerConfig.Erg.Pkgname = "erg"
}
return &sqlBoilerConfig, nil
}
func parseModFile() (*modfile.File, error) {
b, err := os.ReadFile("go.mod")
if err != nil {
return nil, err
}
f, err := modfile.Parse("go.mod", b, nil)
if err != nil {
return nil, err
}
return f, nil
}