forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
292 lines (249 loc) · 10.5 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Package main defines a command line interface for the sqlboiler package
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/friendsofgo/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/volatiletech/sqlboiler/v4/boilingcore"
"github.com/volatiletech/sqlboiler/v4/drivers"
"github.com/volatiletech/sqlboiler/v4/importers"
)
const sqlBoilerVersion = "4.14.2"
var (
flagConfigFile string
cmdState *boilingcore.State
cmdConfig *boilingcore.Config
)
func initConfig() {
if len(flagConfigFile) != 0 {
viper.SetConfigFile(flagConfigFile)
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
return
}
var err error
viper.SetConfigName("sqlboiler")
configHome := os.Getenv("XDG_CONFIG_HOME")
homePath := os.Getenv("HOME")
wd, err := os.Getwd()
if err != nil {
wd = "."
}
configPaths := []string{wd}
if len(configHome) > 0 {
configPaths = append(configPaths, filepath.Join(configHome, "sqlboiler"))
} else {
configPaths = append(configPaths, filepath.Join(homePath, ".config/sqlboiler"))
}
for _, p := range configPaths {
viper.AddConfigPath(p)
}
// Ignore errors here, fallback to other validation methods.
// Users can use environment variables if a config is not found.
_ = viper.ReadInConfig()
}
func main() {
// Too much happens between here and cobra's argument handling, for
// something so simple just do it immediately.
for _, arg := range os.Args {
if arg == "--version" {
fmt.Println("SQLBoiler v" + sqlBoilerVersion)
return
}
}
// Set up the cobra root command
rootCmd := &cobra.Command{
Use: "sqlboiler [flags] <driver>",
Short: "SQL Boiler generates an ORM tailored to your database schema.",
Long: "SQL Boiler generates a Go ORM from template files, tailored to your database schema.\n" +
`Complete documentation is available at http://github.com/volatiletech/sqlboiler`,
Example: `sqlboiler psql`,
PreRunE: preRun,
RunE: run,
PostRunE: postRun,
SilenceErrors: true,
SilenceUsage: true,
}
cobra.OnInitialize(initConfig)
// Set up the cobra root command flags
rootCmd.PersistentFlags().StringVarP(&flagConfigFile, "config", "c", "", "Filename of config file to override default lookup")
rootCmd.PersistentFlags().StringP("output", "o", "models", "The name of the folder to output to")
rootCmd.PersistentFlags().StringP("pkgname", "p", "models", "The name you wish to assign to your generated package")
rootCmd.PersistentFlags().StringSliceP("templates", "", nil, "A templates directory, overrides the embedded template folders in sqlboiler")
rootCmd.PersistentFlags().StringSliceP("tag", "t", nil, "Struct tags to be included on your models in addition to json, yaml, toml")
rootCmd.PersistentFlags().StringSliceP("replace", "", nil, "Replace templates by directory: relpath/to_file.tpl:relpath/to_replacement.tpl")
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug mode prints stack traces on error")
rootCmd.PersistentFlags().BoolP("no-context", "", false, "Disable context.Context usage in the generated code")
rootCmd.PersistentFlags().BoolP("no-tests", "", false, "Disable generated go test files")
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models")
rootCmd.PersistentFlags().BoolP("no-rows-affected", "", false, "Disable rows affected in the generated API")
rootCmd.PersistentFlags().BoolP("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at")
rootCmd.PersistentFlags().BoolP("no-driver-templates", "", false, "Disable parsing of templates defined by the database driver")
rootCmd.PersistentFlags().BoolP("no-back-referencing", "", false, "Disable back referencing in the loaded relationship structs")
rootCmd.PersistentFlags().BoolP("always-wrap-errors", "", false, "Wrap all returned errors with stacktraces, also sql.ErrNoRows")
rootCmd.PersistentFlags().BoolP("add-global-variants", "", false, "Enable generation for global variants")
rootCmd.PersistentFlags().BoolP("add-panic-variants", "", false, "Enable generation for panic variants")
rootCmd.PersistentFlags().BoolP("add-soft-deletes", "", false, "Enable soft deletion by updating deleted_at timestamp")
rootCmd.PersistentFlags().BoolP("add-enum-types", "", false, "Enable generation of types for enums")
rootCmd.PersistentFlags().StringP("enum-null-prefix", "", "Null", "Name prefix of nullable enum types")
rootCmd.PersistentFlags().BoolP("version", "", false, "Print the version")
rootCmd.PersistentFlags().BoolP("wipe", "", false, "Delete the output folder (rm -rf) before generation to ensure sanity")
rootCmd.PersistentFlags().StringP("struct-tag-casing", "", "snake", "Decides the casing for go structure tag names. camel, title or snake (default snake)")
rootCmd.PersistentFlags().StringP("relation-tag", "r", "-", "Relationship struct tag name")
rootCmd.PersistentFlags().StringSliceP("tag-ignore", "", nil, "List of column names that should have tags values set to '-' (ignored during parsing)")
// hide flags not recommended for use
rootCmd.PersistentFlags().MarkHidden("replace")
viper.BindPFlags(rootCmd.PersistentFlags())
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := rootCmd.Execute(); err != nil {
if e, ok := err.(commandFailure); ok {
fmt.Printf("Error: %v\n\n", string(e))
rootCmd.Help()
} else if !viper.GetBool("debug") {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Error: %+v\n", err)
}
os.Exit(1)
}
}
type commandFailure string
func (c commandFailure) Error() string {
return string(c)
}
func preRun(cmd *cobra.Command, args []string) error {
var err error
if len(args) == 0 {
return commandFailure("must provide a driver name")
}
driverName, driverPath, err := drivers.RegisterBinaryFromCmdArg(args[0])
if err != nil {
return errors.Wrap(err, "could not register driver")
}
cmdConfig = &boilingcore.Config{
DriverName: driverName,
OutFolder: viper.GetString("output"),
PkgName: viper.GetString("pkgname"),
Debug: viper.GetBool("debug"),
AddGlobal: viper.GetBool("add-global-variants"),
AddPanic: viper.GetBool("add-panic-variants"),
AddSoftDeletes: viper.GetBool("add-soft-deletes"),
AddEnumTypes: viper.GetBool("add-enum-types"),
EnumNullPrefix: viper.GetString("enum-null-prefix"),
NoContext: viper.GetBool("no-context"),
NoTests: viper.GetBool("no-tests"),
NoHooks: viper.GetBool("no-hooks"),
NoRowsAffected: viper.GetBool("no-rows-affected"),
NoAutoTimestamps: viper.GetBool("no-auto-timestamps"),
NoDriverTemplates: viper.GetBool("no-driver-templates"),
NoBackReferencing: viper.GetBool("no-back-referencing"),
AlwaysWrapErrors: viper.GetBool("always-wrap-errors"),
Wipe: viper.GetBool("wipe"),
StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake | title
TagIgnore: viper.GetStringSlice("tag-ignore"),
RelationTag: viper.GetString("relation-tag"),
TemplateDirs: viper.GetStringSlice("templates"),
Tags: viper.GetStringSlice("tag"),
Replacements: viper.GetStringSlice("replace"),
Aliases: boilingcore.ConvertAliases(viper.Get("aliases")),
TypeReplaces: boilingcore.ConvertTypeReplace(viper.Get("types")),
AutoColumns: boilingcore.AutoColumns{
Created: viper.GetString("auto-columns.created"),
Updated: viper.GetString("auto-columns.updated"),
Deleted: viper.GetString("auto-columns.deleted"),
},
Inflections: boilingcore.Inflections{
Plural: viper.GetStringMapString("inflections.plural"),
PluralExact: viper.GetStringMapString("inflections.plural_exact"),
Singular: viper.GetStringMapString("inflections.singular"),
SingularExact: viper.GetStringMapString("inflections.singular_exact"),
Irregular: viper.GetStringMapString("inflections.irregular"),
},
Version: sqlBoilerVersion,
}
if cmdConfig.Debug {
fmt.Fprintln(os.Stderr, "using driver:", driverPath)
}
// Configure the driver
cmdConfig.DriverConfig = map[string]interface{}{
"whitelist": viper.GetStringSlice(driverName + ".whitelist"),
"blacklist": viper.GetStringSlice(driverName + ".blacklist"),
"add-enum-types": cmdConfig.AddEnumTypes,
"enum-null-prefix": cmdConfig.EnumNullPrefix,
}
keys := allKeys(driverName)
for _, key := range keys {
if key != "blacklist" && key != "whitelist" {
prefixedKey := fmt.Sprintf("%s.%s", driverName, key)
cmdConfig.DriverConfig[key] = viper.Get(prefixedKey)
}
}
cmdConfig.Imports = configureImports()
cmdState, err = boilingcore.New(cmdConfig)
return err
}
func configureImports() importers.Collection {
imports := importers.NewDefaultImports()
mustMap := func(m importers.Map, err error) importers.Map {
if err != nil {
panic("failed to change viper interface into importers.Map: " + err.Error())
}
return m
}
if viper.IsSet("imports.all.standard") {
imports.All.Standard = viper.GetStringSlice("imports.all.standard")
}
if viper.IsSet("imports.all.third_party") {
imports.All.ThirdParty = viper.GetStringSlice("imports.all.third_party")
}
if viper.IsSet("imports.test.standard") {
imports.Test.Standard = viper.GetStringSlice("imports.test.standard")
}
if viper.IsSet("imports.test.third_party") {
imports.Test.ThirdParty = viper.GetStringSlice("imports.test.third_party")
}
if viper.IsSet("imports.singleton") {
imports.Singleton = mustMap(importers.MapFromInterface(viper.Get("imports.singleton")))
}
if viper.IsSet("imports.test_singleton") {
imports.TestSingleton = mustMap(importers.MapFromInterface(viper.Get("imports.test_singleton")))
}
if viper.IsSet("imports.based_on_type") {
imports.BasedOnType = mustMap(importers.MapFromInterface(viper.Get("imports.based_on_type")))
}
return imports
}
func run(cmd *cobra.Command, args []string) error {
return cmdState.Run()
}
func postRun(cmd *cobra.Command, args []string) error {
return cmdState.Cleanup()
}
func allKeys(prefix string) []string {
keys := make(map[string]bool)
prefix += "."
for _, e := range os.Environ() {
splits := strings.SplitN(e, "=", 2)
key := strings.ReplaceAll(strings.ToLower(splits[0]), "_", ".")
if strings.HasPrefix(key, prefix) {
keys[strings.ReplaceAll(key, prefix, "")] = true
}
}
for _, key := range viper.AllKeys() {
if strings.HasPrefix(key, prefix) {
keys[strings.ReplaceAll(key, prefix, "")] = true
}
}
keySlice := make([]string, 0, len(keys))
for k := range keys {
keySlice = append(keySlice, k)
}
return keySlice
}