forked from gobackup/gobackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
152 lines (130 loc) · 3.29 KB
/
config.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
package config
import (
"fmt"
"os"
"path"
"time"
"github.com/huacnlee/gobackup/logger"
"github.com/spf13/viper"
)
var (
// Exist Is config file exist
Exist bool
// Models configs
Models []ModelConfig
// HomeDir of user
HomeDir = os.Getenv("HOME")
)
// ModelConfig for special case
type ModelConfig struct {
Name string
TempPath string
DumpPath string
CompressWith SubConfig
EncryptWith SubConfig
StoreWith SubConfig
Archive *viper.Viper
Databases []SubConfig
Storages []SubConfig
Viper *viper.Viper
}
// SubConfig sub config info
type SubConfig struct {
Name string
Type string
Viper *viper.Viper
}
// loadConfig from:
// - ./gobackup.yml
// - ~/.gobackup/gobackup.yml
// - /etc/gobackup/gobackup.yml
func Init(configFile string) {
viper.SetConfigType("yaml")
// set config file directly
if len(configFile) > 0 {
viper.SetConfigFile(configFile)
} else {
viper.SetConfigName("gobackup")
// ./gobackup.yml
viper.AddConfigPath(".")
// ~/.gobackup/gobackup.yml
viper.AddConfigPath("$HOME/.gobackup") // call multiple times to add many search paths
// /etc/gobackup/gobackup.yml
viper.AddConfigPath("/etc/gobackup/") // path to look for the config file in
}
err := viper.ReadInConfig()
if err != nil {
logger.Error("Load gobackup config faild", err)
return
}
viper.SetDefault("workdir", path.Join(os.TempDir(), "gobackup"))
Exist = true
Models = []ModelConfig{}
for key := range viper.GetStringMap("models") {
Models = append(Models, loadModel(key))
}
}
func loadModel(key string) (model ModelConfig) {
model.Name = key
model.TempPath = path.Join(viper.GetString("workdir"), fmt.Sprintf("%d", time.Now().UnixNano()))
model.DumpPath = path.Join(model.TempPath, key)
model.Viper = viper.Sub("models." + key)
model.CompressWith = SubConfig{
Type: model.Viper.GetString("compress_with.type"),
Viper: model.Viper.Sub("compress_with"),
}
model.EncryptWith = SubConfig{
Type: model.Viper.GetString("encrypt_with.type"),
Viper: model.Viper.Sub("encrypt_with"),
}
model.StoreWith = SubConfig{
Type: model.Viper.GetString("store_with.type"),
Viper: model.Viper.Sub("store_with"),
}
model.Archive = model.Viper.Sub("archive")
loadDatabasesConfig(&model)
loadStoragesConfig(&model)
return
}
func loadDatabasesConfig(model *ModelConfig) {
subViper := model.Viper.Sub("databases")
for key := range model.Viper.GetStringMap("databases") {
dbViper := subViper.Sub(key)
model.Databases = append(model.Databases, SubConfig{
Name: key,
Type: dbViper.GetString("type"),
Viper: dbViper,
})
}
}
func loadStoragesConfig(model *ModelConfig) {
subViper := model.Viper.Sub("storages")
for key := range model.Viper.GetStringMap("storages") {
dbViper := subViper.Sub(key)
model.Storages = append(model.Storages, SubConfig{
Name: key,
Type: dbViper.GetString("type"),
Viper: dbViper,
})
}
}
// GetModelByName get model by name
func GetModelByName(name string) (model *ModelConfig) {
for _, m := range Models {
if m.Name == name {
model = &m
return
}
}
return
}
// GetDatabaseByName get database config by name
func (model *ModelConfig) GetDatabaseByName(name string) (subConfig *SubConfig) {
for _, m := range model.Databases {
if m.Name == name {
subConfig = &m
return
}
}
return
}