1
|
|
|
package settings |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"os" |
6
|
|
|
"path/filepath" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// These database types are supported. |
10
|
|
|
const ( |
11
|
|
|
DbTypePostgresql DbType = "pg" |
12
|
|
|
DbTypeMySQL DbType = "mysql" |
13
|
|
|
DbTypeSQLite DbType = "sqlite3" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
// DbType represents a type of a database. |
17
|
|
|
type DbType string |
18
|
|
|
|
19
|
|
|
// Set sets the datatype for the custom type for the flag package. |
20
|
|
|
func (db *DbType) Set(s string) error { |
21
|
1 |
|
*db = DbType(s) |
22
|
1 |
|
if *db == "" { |
23
|
1 |
|
*db = DbTypePostgresql |
24
|
|
|
} |
25
|
1 |
|
if !SupportedDbTypes[*db] { |
26
|
1 |
|
return fmt.Errorf("type of database %q not supported! supported: %v", *db, SprintfSupportedDbTypes()) |
27
|
|
|
} |
28
|
1 |
|
return nil |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// String is the implementation of the Stringer interface needed for flag.Value interface. |
32
|
|
|
func (db DbType) String() string { |
33
|
1 |
|
return string(db) |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// These null types are supported. The types native and primitive map to the same |
37
|
|
|
// underlying builtin golang type. |
38
|
|
|
const ( |
39
|
|
|
NullTypeSQL NullType = "sql" |
40
|
|
|
NullTypeNative NullType = "native" |
41
|
|
|
NullTypePrimitive NullType = "primitive" |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
// NullType represents a null type. |
45
|
|
|
type NullType string |
46
|
|
|
|
47
|
|
|
// Set sets the datatype for the custom type for the flag package. |
48
|
|
|
func (t *NullType) Set(s string) error { |
49
|
1 |
|
*t = NullType(s) |
50
|
1 |
|
if *t == "" { |
51
|
1 |
|
*t = NullTypeSQL |
52
|
|
|
} |
53
|
1 |
|
if !supportedNullTypes[*t] { |
54
|
1 |
|
return fmt.Errorf("null type %q not supported! supported: %v", *t, SprintfSupportedNullTypes()) |
55
|
|
|
} |
56
|
1 |
|
return nil |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// String is the implementation of the Stringer interface needed for flag.Value interface. |
60
|
|
|
func (t NullType) String() string { |
61
|
1 |
|
return string(t) |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// These are the output format command line parameter. |
65
|
|
|
const ( |
66
|
|
|
OutputFormatCamelCase OutputFormat = "c" |
67
|
|
|
OutputFormatOriginal OutputFormat = "o" |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
// OutputFormat represents a output format option. |
71
|
|
|
type OutputFormat string |
72
|
|
|
|
73
|
|
|
// Set sets the datatype for the custom type for the flag package. |
74
|
|
|
func (of *OutputFormat) Set(s string) error { |
75
|
1 |
|
*of = OutputFormat(s) |
76
|
1 |
|
if *of == "" { |
77
|
1 |
|
*of = OutputFormatCamelCase |
78
|
|
|
} |
79
|
1 |
|
if !supportedOutputFormats[*of] { |
80
|
1 |
|
return fmt.Errorf("output format %q not supported", *of) |
81
|
|
|
} |
82
|
1 |
|
return nil |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// String is the implementation of the Stringer interface needed for flag.Value interface. |
86
|
|
|
func (of OutputFormat) String() string { |
87
|
1 |
|
return string(of) |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// These are the filename format command line parameter. |
91
|
|
|
const ( |
92
|
|
|
FileNameFormatCamelCase FileNameFormat = "c" |
93
|
|
|
FileNameFormatSnakeCase FileNameFormat = "s" |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
// FileNameFormat represents a output filename format |
97
|
|
|
type FileNameFormat string |
98
|
|
|
|
99
|
|
|
func (of *FileNameFormat) Set(s string) error { |
100
|
1 |
|
*of = FileNameFormat(s) |
101
|
1 |
|
if *of == "" { |
102
|
1 |
|
*of = FileNameFormatCamelCase |
103
|
|
|
} |
104
|
1 |
|
if !supportedFileNameFormats[*of] { |
105
|
1 |
|
return fmt.Errorf("filename format %q not supported", *of) |
106
|
|
|
} |
107
|
1 |
|
return nil |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
func (of FileNameFormat) String() string { |
111
|
1 |
|
return string(of) |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
var ( |
115
|
|
|
// SupportedDbTypes represents the supported databases |
116
|
|
|
SupportedDbTypes = map[DbType]bool{ |
117
|
|
|
DbTypePostgresql: true, |
118
|
|
|
DbTypeMySQL: true, |
119
|
|
|
DbTypeSQLite: true, |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// supportedOutputFormats represents the supported output formats |
123
|
|
|
supportedOutputFormats = map[OutputFormat]bool{ |
124
|
|
|
OutputFormatCamelCase: true, |
125
|
|
|
OutputFormatOriginal: true, |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// dbDefaultPorts maps the database type to the default ports |
129
|
|
|
dbDefaultPorts = map[DbType]string{ |
130
|
|
|
DbTypePostgresql: "5432", |
131
|
|
|
DbTypeMySQL: "3306", |
132
|
|
|
DbTypeSQLite: "", |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// supportedNullTypes represents the supported types of NULL types |
136
|
|
|
supportedNullTypes = map[NullType]bool{ |
137
|
|
|
NullTypeSQL: true, |
138
|
|
|
NullTypeNative: true, |
139
|
|
|
NullTypePrimitive: true, |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
supportedFileNameFormats = map[FileNameFormat]bool{ |
143
|
|
|
FileNameFormatCamelCase: true, |
144
|
|
|
FileNameFormatSnakeCase: true, |
145
|
|
|
} |
146
|
|
|
) |
147
|
|
|
|
148
|
|
|
// Settings stores the supported settings / command line arguments |
149
|
|
|
type Settings struct { |
150
|
|
|
Verbose bool |
151
|
|
|
VVerbose bool |
152
|
|
|
Force bool // continue through errors |
153
|
|
|
|
154
|
|
|
DbType DbType |
155
|
|
|
|
156
|
|
|
User string |
157
|
|
|
Pswd string |
158
|
|
|
DbName string |
159
|
|
|
Schema string |
160
|
|
|
Host string |
161
|
|
|
Port string |
162
|
|
|
|
163
|
|
|
OutputFilePath string |
164
|
|
|
OutputFormat OutputFormat |
165
|
|
|
|
166
|
|
|
FileNameFormat FileNameFormat |
167
|
|
|
PackageName string |
168
|
|
|
Prefix string |
169
|
|
|
Suffix string |
170
|
|
|
Null NullType |
171
|
|
|
|
172
|
|
|
NoInitialism bool |
173
|
|
|
|
174
|
|
|
TagsNoDb bool |
175
|
|
|
|
176
|
|
|
TagsMastermindStructable bool |
177
|
|
|
TagsMastermindStructableOnly bool |
178
|
|
|
IsMastermindStructableRecorder bool |
179
|
|
|
|
180
|
|
|
// TODO not implemented yet |
181
|
|
|
TagsGorm bool |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// New constructs settings with default values |
185
|
|
|
func New() *Settings { |
186
|
|
|
|
187
|
1 |
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) |
188
|
1 |
|
if err != nil { |
189
|
|
|
dir = "." |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
return &Settings{ |
193
|
|
|
Verbose: false, |
194
|
|
|
VVerbose: false, |
195
|
|
|
Force: false, |
196
|
|
|
|
197
|
|
|
DbType: DbTypePostgresql, |
198
|
|
|
User: "", |
199
|
|
|
Pswd: "", |
200
|
|
|
DbName: "postgres", |
201
|
|
|
Schema: "public", |
202
|
|
|
Host: "127.0.0.1", |
203
|
|
|
Port: "", // left blank -> is automatically determined if not set |
204
|
|
|
OutputFilePath: dir, |
205
|
|
|
OutputFormat: OutputFormatCamelCase, |
206
|
|
|
FileNameFormat: FileNameFormatCamelCase, |
207
|
|
|
PackageName: "dto", |
208
|
|
|
Prefix: "", |
209
|
|
|
Suffix: "", |
210
|
|
|
Null: NullTypeSQL, |
211
|
|
|
|
212
|
|
|
NoInitialism: false, |
213
|
|
|
|
214
|
|
|
TagsNoDb: false, |
215
|
|
|
|
216
|
|
|
TagsMastermindStructable: false, |
217
|
|
|
TagsMastermindStructableOnly: false, |
218
|
|
|
IsMastermindStructableRecorder: false, |
219
|
|
|
|
220
|
|
|
TagsGorm: false, |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Verify verifies the settings and checks the given output paths |
225
|
|
|
func (settings *Settings) Verify() (err error) { |
226
|
|
|
|
227
|
1 |
|
if err = settings.verifyOutputPath(); err != nil { |
228
|
1 |
|
return err |
229
|
|
|
} |
230
|
|
|
|
231
|
1 |
|
if settings.OutputFilePath, err = settings.prepareOutputPath(); err != nil { |
232
|
|
|
return err |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
if settings.Port == "" { |
236
|
1 |
|
settings.Port = dbDefaultPorts[settings.DbType] |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
if settings.PackageName == "" { |
240
|
1 |
|
return fmt.Errorf("name of package can not be empty") |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
if settings.VVerbose { |
244
|
1 |
|
settings.Verbose = true |
245
|
|
|
} |
246
|
|
|
|
247
|
1 |
|
return err |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
func (settings *Settings) verifyOutputPath() (err error) { |
251
|
|
|
|
252
|
1 |
|
info, err := os.Stat(settings.OutputFilePath) |
253
|
|
|
|
254
|
1 |
|
if os.IsNotExist(err) { |
255
|
1 |
|
return fmt.Errorf("output file path %q does not exists", settings.OutputFilePath) |
256
|
|
|
} |
257
|
|
|
|
258
|
1 |
|
if !info.Mode().IsDir() { |
259
|
1 |
|
return fmt.Errorf("output file path %q is not a directory", settings.OutputFilePath) |
260
|
|
|
} |
261
|
|
|
|
262
|
1 |
|
return err |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
func (settings *Settings) prepareOutputPath() (outputFilePath string, err error) { |
266
|
1 |
|
outputFilePath, err = filepath.Abs(settings.OutputFilePath) |
267
|
1 |
|
outputFilePath += string(filepath.Separator) |
268
|
1 |
|
return outputFilePath, err |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// SprintfSupportedDbTypes returns a slice of strings as names of the supported database types |
272
|
|
|
func SprintfSupportedDbTypes() string { |
273
|
1 |
|
names := make([]string, 0, len(SupportedDbTypes)) |
274
|
1 |
|
for name := range SupportedDbTypes { |
275
|
1 |
|
names = append(names, string(name)) |
276
|
|
|
} |
277
|
1 |
|
return fmt.Sprintf("%v", names) |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// SprintfSupportedNullTypes returns a slice of strings as names of the supported null types |
281
|
|
|
func SprintfSupportedNullTypes() string { |
282
|
1 |
|
names := make([]string, 0, len(supportedNullTypes)) |
283
|
1 |
|
for name := range supportedNullTypes { |
284
|
1 |
|
names = append(names, string(name)) |
285
|
|
|
} |
286
|
1 |
|
return fmt.Sprintf("%v", names) |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// IsNullTypeSQL returns true if the type given by the command line args is of null type SQL |
290
|
|
|
func (settings *Settings) IsNullTypeSQL() bool { |
291
|
1 |
|
return settings.Null == NullTypeSQL |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// ShouldInitialism returns wheather or not if column names should be converted |
295
|
|
|
// to initialisms. |
296
|
|
|
func (settings *Settings) ShouldInitialism() bool { |
297
|
1 |
|
return !settings.NoInitialism |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// IsOutputFormatCamelCase returns if the type given by command line args is of camel-case format. |
301
|
|
|
func (settings *Settings) IsOutputFormatCamelCase() bool { |
302
|
1 |
|
return settings.OutputFormat == OutputFormatCamelCase |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// IsFileNameFormatSnakeCase returns if the type given by the command line args is snake-case format |
306
|
|
|
func (settings *Settings) IsFileNameFormatSnakeCase() bool { |
307
|
1 |
|
return settings.FileNameFormat == FileNameFormatSnakeCase |
308
|
|
|
} |
309
|
|
|
|