-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.go
342 lines (299 loc) · 8 KB
/
startup.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package startup
import (
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"github.com/KusoKaihatsuSha/startup/internal/helpers"
"github.com/KusoKaihatsuSha/startup/internal/order"
tags "github.com/KusoKaihatsuSha/startup/internal/tag"
"github.com/KusoKaihatsuSha/startup/internal/validation"
)
var (
this any
onceFlags = sync.Once{}
)
var DEBUG = false
const (
// FLAG Get data from flags
FLAG = order.FLAG
// FILE Get data from json file
FILE = order.FILE
// ENV Get data from environments
ENV = order.ENV
// PreloadConfigEnvThenFlag - Get filepath config from environments and then flags
// Default
PreloadConfigEnvThenFlag = order.PreloadConfigEnvThenFlag
// PreloadConfigFlagThenEnv - Get filepath config from flags and then environments
PreloadConfigFlagThenEnv = order.PreloadConfigFlagThenEnv
// PreloadConfigFlag - Get filepath config from flags
PreloadConfigFlag = order.PreloadConfigFlag
// PreloadConfigEnv - Get filepath config from environments
PreloadConfigEnv = order.PreloadConfigEnv
// NoPreloadConfig - Get filepath config file only from ordered stages
NoPreloadConfig = order.NoPreloadConfig
)
// Concat structs
type temp[T any] struct {
Stages []order.Stages
tags.Tags
CustomerConfiguration T
Configuration configuration
}
/*
Configuration consists of settings that are filled in at startup.
Default fields:
- "Config" - filepath for config file
*/
type configuration struct {
Config string `json:"startup_configuration_file" default:"config.ini" flag:"config" env:"CONFIG" help:"Configuration settings file" valid:"default_configuration_file"`
}
/*
AddValidation using for add custom validation
Example:
// Custom struct. Struct will be implement in program with selected 'Stages' variable.
type Test struct {
NewValid []string `json:"new-valid" default:"new valid is default" flag:"valid" text:"-" valid:"test"`
}
// Custom type.
type testValid string
// Custom validation.
var testValidation testValid = "test"
// Custom method.
func (o testValid) Valid(stringValue string, value any) (any, bool) {
return []string{stringValue + "+++"}, true
}
// add custom validation
func MyFunc() {
...
startup.AddValidation(testValidation)
...
// Implement all types of configs (Json file -> Environment -> Flags).
configurations := startup.Get[Test](startup.FILE, startup.ENV, startup.FLAG)
// Test print.
fmt.Println(configurations)
}
Default validations:
- `tmp_file` - Check exist inside Temp folder and create if not exist (string in struct)
- `file` - Check exist the filepath and create if not exist (string in struct)
- `url` - Check url is correct (string in struct)
- `bool` - Parse Bool (bool in struct)
- `int` - Parse int (int64 in struct)
- `float` - Parse float (float64 in struct)
- `duration` - Parse duration (time.Duration in struct)
- `uuid` - Check uuid. Return new if not exist (string in struct)
Caution:
flags are reserved:
- config
*/
func AddValidation(value ...validation.Valid) {
validation.Add(value...)
}
/*
GetForce will initialize scan the flags, environment and config-file with the right order:
- order.FLAG - flag
- order.FILE - config file
- order.ENV - environment
Caution! flags are reserved:
- config
*/
func GetForce[T any](stages ...order.Stages) T {
// ---debug---
if DEBUG {
helpers.PrintDebug(stages...)
}
// ---debug---
return get[T](stages...).CustomerConfiguration
}
func (t *temp[T]) prepare(config tags.Tags) *temp[T] {
elements := reflect.ValueOf(&t.CustomerConfiguration).Elem()
t.Tags = make(tags.Tags, elements.NumField())
for ii := 0; ii < elements.NumField(); ii++ {
name := elements.Type().Field(ii).Name
t.Tags[name] = tags.Fill[T](name, t.Stages...)
}
for configTagName, configTagData := range config {
t.Tags[configTagName] = configTagData
}
return t
}
func (t *temp[T]) preparePreload() *temp[T] {
elements := reflect.ValueOf(&t.Configuration).Elem()
t.Tags = make(tags.Tags, elements.NumField())
for ii := 0; ii < elements.NumField(); ii++ {
name := elements.Type().Field(ii).Name
t.Tags[name] = tags.Fill[configuration](name, t.Stages...)
}
return t
}
func (t *temp[T]) fillPreload(fileExist bool) *temp[T] {
for _, v := range t.Stages {
switch v {
case order.FLAG:
// ---debug---
if DEBUG && fileExist {
printing := ""
for _, arg := range os.Args {
if strings.Contains(arg, "-config=") {
printing = "\tinfo about config file:\tGet filepath from flag '-config'"
break
} else {
printing = "\tinfo about config file:\tFlag '-config' with filepath not set"
}
}
fmt.Println(printing)
}
// ---debug---
t.flagNoParse()
case order.FILE:
t.conf()
case order.ENV:
// ---debug---
if DEBUG && fileExist {
if os.Getenv("CONFIG") != "" {
fmt.Println("\tinfo about config file:\tGet filepath from environment 'CONFIG'")
} else {
fmt.Println("\tinfo about config file:\tEnvironment 'CONFIG' with filepath not set")
}
}
// ---debug---
t.env()
}
}
return t
}
func (t *temp[T]) fill() *temp[T] {
for _, v := range t.Stages {
switch v {
case order.FLAG:
t.flag()
case order.FILE:
t.conf()
case order.ENV:
t.env()
}
}
return t
}
func get[T any](stages ...order.Stages) temp[T] {
fileExistInStages := helpers.FileConfExistInStages(stages...)
preload := (&temp[T]{
Stages: helpers.PresetPreload(stages...),
CustomerConfiguration: *new(T),
Configuration: configuration{},
}).
preparePreload().
fillPreload(fileExistInStages).
conf()
// ---debug---
if DEBUG && fileExistInStages {
cfg := ""
switch {
case preload.Configuration.Config == "config.ini":
if helpers.FileExist(preload.Configuration.Config) {
cfg = "default config.ini"
} else {
cfg = "skipped default config.ini(not exist)"
}
case len(preload.Configuration.Config) > 0:
if helpers.FileExist(preload.Configuration.Config) {
// custom config file
cfg = filepath.Base(preload.Configuration.Config)
} else {
cfg = "skipped config file(not exist)"
}
default:
cfg = "not any config file"
}
fmt.Printf("FILE => %s\n", cfg)
}
// ---debug---
load := (&temp[T]{
Stages: stages,
CustomerConfiguration: *new(T),
Configuration: preload.Configuration,
}).prepare(preload.Tags)
load.
fill().
valid()
// ---debug---
if DEBUG {
fmt.Printf("DATA => %v\n\n", load.CustomerConfiguration)
}
// ---debug---
return *load
}
/*
Get will initialize scan the flags(one time), environment and config-file with the right order:
- order.FLAG - flag
- order.FILE - config file
- order.ENV - environment
Caution! flags are reserved:
- config
*/
func Get[T any](stages ...order.Stages) T {
onceFlags.Do(
func() {
this = get[T](stages...)
})
return this.(temp[T]).CustomerConfiguration
}
func (t *temp[T]) dummy() *temp[T] {
for _, v := range t.Tags {
v.DummyFlags()
}
flag.Parse()
return t
}
func (t *temp[T]) flag() *temp[T] {
for _, v := range t.Tags {
v.Flag()
}
return t.dummy()
}
func (t *temp[T]) flagNoParse() *temp[T] {
for _, v := range t.Tags {
v.Flag()
}
return t
}
func (t *temp[T]) env() *temp[T] {
for _, v := range t.Tags {
v.Env()
}
return t
}
// valid check info and make some correcting
func (t *temp[T]) valid() *temp[T] {
for k, v := range t.Tags {
field := reflect.ValueOf(&t.CustomerConfiguration).Elem().FieldByName(k)
if field.CanSet() {
if valid := v.Valid(); valid != nil {
field.Set(reflect.ValueOf(valid))
} else {
field.SetZero()
}
}
}
return t
}
// conf get info from the configuration file
func (t *temp[T]) conf() *temp[T] {
confFile := ""
for _, f := range t.Tags["Config"].Flags {
if f.Value.String() != "" {
confFile = f.Value.String()
}
}
if confFile != "" {
reflect.ValueOf(&t.Configuration).Elem().FieldByName("Config").Set(reflect.ValueOf(t.Tags["Config"].Valid()))
tmpConfig := helpers.SettingsFile(t.Configuration.Config)
for _, v := range t.Tags {
v.ConfigFile(tmpConfig)
}
}
return t
}