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
|
|
|
// Set sets the datatype for the custom type for the flag package. |
100
|
|
|
func (of *FileNameFormat) Set(s string) error { |
101
|
1 |
|
*of = FileNameFormat(s) |
102
|
1 |
|
if *of == "" { |
103
|
1 |
|
*of = FileNameFormatCamelCase |
104
|
|
|
} |
105
|
1 |
|
if !supportedFileNameFormats[*of] { |
106
|
1 |
|
return fmt.Errorf("filename format %q not supported", *of) |
107
|
|
|
} |
108
|
1 |
|
return nil |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
func (of FileNameFormat) String() string { |
112
|
1 |
|
return string(of) |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
var ( |
116
|
|
|
// SupportedDbTypes represents the supported databases |
117
|
|
|
SupportedDbTypes = map[DbType]bool{ |
118
|
|
|
DbTypePostgresql: true, |
119
|
|
|
DbTypeMySQL: true, |
120
|
|
|
DbTypeSQLite: true, |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// supportedOutputFormats represents the supported output formats |
124
|
|
|
supportedOutputFormats = map[OutputFormat]bool{ |
125
|
|
|
OutputFormatCamelCase: true, |
126
|
|
|
OutputFormatOriginal: true, |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// dbDefaultPorts maps the database type to the default ports |
130
|
|
|
dbDefaultPorts = map[DbType]string{ |
131
|
|
|
DbTypePostgresql: "5432", |
132
|
|
|
DbTypeMySQL: "3306", |
133
|
|
|
DbTypeSQLite: "", |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// supportedNullTypes represents the supported types of NULL types |
137
|
|
|
supportedNullTypes = map[NullType]bool{ |
138
|
|
|
NullTypeSQL: true, |
139
|
|
|
NullTypeNative: true, |
140
|
|
|
NullTypePrimitive: true, |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// supportedFileNameFormats represents the supported filename formats |
144
|
|
|
supportedFileNameFormats = map[FileNameFormat]bool{ |
145
|
|
|
FileNameFormatCamelCase: true, |
146
|
|
|
FileNameFormatSnakeCase: true, |
147
|
|
|
} |
148
|
|
|
) |
149
|
|
|
|
150
|
|
|
// Settings stores the supported settings / command line arguments |
151
|
|
|
type Settings struct { |
152
|
|
|
Verbose bool |
153
|
|
|
VVerbose bool |
154
|
|
|
Force bool // continue through errors |
155
|
|
|
|
156
|
|
|
DbType DbType |
157
|
|
|
|
158
|
|
|
User string |
159
|
|
|
Pswd string |
160
|
|
|
DbName string |
161
|
|
|
Schema string |
162
|
|
|
Host string |
163
|
|
|
Port string |
164
|
|
|
|
165
|
|
|
OutputFilePath string |
166
|
|
|
OutputFormat OutputFormat |
167
|
|
|
|
168
|
|
|
FileNameFormat FileNameFormat |
169
|
|
|
PackageName string |
170
|
|
|
Prefix string |
171
|
|
|
Suffix string |
172
|
|
|
Null NullType |
173
|
|
|
|
174
|
|
|
NoInitialism bool |
175
|
|
|
|
176
|
|
|
TagsNoDb bool |
177
|
|
|
|
178
|
|
|
TagsMastermindStructable bool |
179
|
|
|
TagsMastermindStructableOnly bool |
180
|
|
|
IsMastermindStructableRecorder bool |
181
|
|
|
|
182
|
|
|
// TODO not implemented yet |
183
|
|
|
TagsGorm bool |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// New constructs settings with default values |
187
|
|
|
func New() *Settings { |
188
|
|
|
|
189
|
1 |
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) |
190
|
1 |
|
if err != nil { |
191
|
|
|
dir = "." |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
return &Settings{ |
195
|
|
|
Verbose: false, |
196
|
|
|
VVerbose: false, |
197
|
|
|
Force: false, |
198
|
|
|
|
199
|
|
|
DbType: DbTypePostgresql, |
200
|
|
|
User: "", |
201
|
|
|
Pswd: "", |
202
|
|
|
DbName: "postgres", |
203
|
|
|
Schema: "public", |
204
|
|
|
Host: "127.0.0.1", |
205
|
|
|
Port: "", // left blank -> is automatically determined if not set |
206
|
|
|
OutputFilePath: dir, |
207
|
|
|
OutputFormat: OutputFormatCamelCase, |
208
|
|
|
FileNameFormat: FileNameFormatCamelCase, |
209
|
|
|
PackageName: "dto", |
210
|
|
|
Prefix: "", |
211
|
|
|
Suffix: "", |
212
|
|
|
Null: NullTypeSQL, |
213
|
|
|
|
214
|
|
|
NoInitialism: false, |
215
|
|
|
|
216
|
|
|
TagsNoDb: false, |
217
|
|
|
|
218
|
|
|
TagsMastermindStructable: false, |
219
|
|
|
TagsMastermindStructableOnly: false, |
220
|
|
|
IsMastermindStructableRecorder: false, |
221
|
|
|
|
222
|
|
|
TagsGorm: false, |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// Verify verifies the settings and checks the given output paths |
227
|
|
|
func (settings *Settings) Verify() (err error) { |
228
|
|
|
|
229
|
1 |
|
if err = settings.verifyOutputPath(); err != nil { |
230
|
1 |
|
return err |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
if settings.OutputFilePath, err = settings.prepareOutputPath(); err != nil { |
234
|
|
|
return err |
235
|
|
|
} |
236
|
|
|
|
237
|
1 |
|
if settings.Port == "" { |
238
|
1 |
|
settings.Port = dbDefaultPorts[settings.DbType] |
239
|
|
|
} |
240
|
|
|
|
241
|
1 |
|
if settings.PackageName == "" { |
242
|
1 |
|
return fmt.Errorf("name of package can not be empty") |
243
|
|
|
} |
244
|
|
|
|
245
|
1 |
|
if settings.VVerbose { |
246
|
1 |
|
settings.Verbose = true |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
return err |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
func (settings *Settings) verifyOutputPath() (err error) { |
253
|
|
|
|
254
|
1 |
|
info, err := os.Stat(settings.OutputFilePath) |
255
|
|
|
|
256
|
1 |
|
if os.IsNotExist(err) { |
257
|
1 |
|
return fmt.Errorf("output file path %q does not exists", settings.OutputFilePath) |
258
|
|
|
} |
259
|
|
|
|
260
|
1 |
|
if !info.Mode().IsDir() { |
261
|
1 |
|
return fmt.Errorf("output file path %q is not a directory", settings.OutputFilePath) |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
return err |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
func (settings *Settings) prepareOutputPath() (outputFilePath string, err error) { |
268
|
1 |
|
outputFilePath, err = filepath.Abs(settings.OutputFilePath) |
269
|
1 |
|
outputFilePath += string(filepath.Separator) |
270
|
1 |
|
return outputFilePath, err |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
// SprintfSupportedDbTypes returns a slice of strings as names of the supported database types |
274
|
|
|
func SprintfSupportedDbTypes() string { |
275
|
1 |
|
names := make([]string, 0, len(SupportedDbTypes)) |
276
|
1 |
|
for name := range SupportedDbTypes { |
277
|
1 |
|
names = append(names, string(name)) |
278
|
|
|
} |
279
|
1 |
|
return fmt.Sprintf("%v", names) |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// SprintfSupportedNullTypes returns a slice of strings as names of the supported null types |
283
|
|
|
func SprintfSupportedNullTypes() string { |
284
|
1 |
|
names := make([]string, 0, len(supportedNullTypes)) |
285
|
1 |
|
for name := range supportedNullTypes { |
286
|
1 |
|
names = append(names, string(name)) |
287
|
|
|
} |
288
|
1 |
|
return fmt.Sprintf("%v", names) |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// IsNullTypeSQL returns true if the type given by the command line args is of null type SQL |
292
|
|
|
func (settings *Settings) IsNullTypeSQL() bool { |
293
|
1 |
|
return settings.Null == NullTypeSQL |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// ShouldInitialism returns wheather or not if column names should be converted |
297
|
|
|
// to initialisms. |
298
|
|
|
func (settings *Settings) ShouldInitialism() bool { |
299
|
1 |
|
return !settings.NoInitialism |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
// IsOutputFormatCamelCase returns if the type given by command line args is of camel-case format. |
303
|
|
|
func (settings *Settings) IsOutputFormatCamelCase() bool { |
304
|
1 |
|
return settings.OutputFormat == OutputFormatCamelCase |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
// IsFileNameFormatSnakeCase returns if the type given by the command line args is snake-case format |
308
|
|
|
func (settings *Settings) IsFileNameFormatSnakeCase() bool { |
309
|
1 |
|
return settings.FileNameFormat == FileNameFormatSnakeCase |
310
|
|
|
} |
311
|
|
|
|