-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
74 lines (59 loc) · 1.55 KB
/
config.go
File metadata and controls
74 lines (59 loc) · 1.55 KB
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
package manager
import (
"fmt"
"github.com/spf13/viper"
"gorm.io/gorm"
)
type configs struct {
Targets map[string]Config
}
var Targets = make(map[string]Target)
type TargetConfigStore interface {
GetConfigs() []TargetConfig
}
type Unmarshaler func(any) error
type TargetConfig interface {
GetPlatform() string
GetUnmarshaler() Unmarshaler
}
type DefaultViperConfigStore struct{}
func (DefaultViperConfigStore) GetConfigs() (configs []TargetConfig) {
viper.SetConfigType("yml")
viper.SetConfigName("org-manager")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %w \n", err))
}
targets := viper.GetStringMap("targets")
for name := range targets {
configs = append(configs, &DefaultViperConfig{targetName: name})
}
return configs
}
type DefaultViperConfig struct {
targetName string
}
func (c DefaultViperConfig) GetPlatform() string {
return viper.GetString(fmt.Sprintf("targets.%s.platform", c.targetName))
}
func (c DefaultViperConfig) GetUnmarshaler() Unmarshaler {
configKey := fmt.Sprintf("targets.%s", c.targetName)
return func(rawVal any) error {
return viper.UnmarshalKey(configKey, rawVal)
}
}
type DatabaseConfigStore struct {
db *gorm.DB
}
func init() {
InitWithTargetConfigStore(&DefaultViperConfigStore{})
}
func InitWithTargetConfigStore(store TargetConfigStore) {
for _, config := range store.GetConfigs() {
target, err := InitTarget(config.GetPlatform(), config.GetUnmarshaler())
if err != nil {
panic(err)
}
Targets[TargetKey(target)] = target
}
}