-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
352 lines (280 loc) 路 8.97 KB
/
conf.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
343
344
345
346
347
348
349
350
351
352
package conf
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/Shitovdm/GitRsync/src/model"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
)
// MarshalFunc returns bytes array.
type MarshalFunc func(v interface{}) ([]byte, error)
// UnmarshalFunc gets bytes array.
type UnmarshalFunc func(data []byte, v interface{}) error
var (
applicationName = ""
formats = map[string]format{}
)
// format struct describes config format.
type format struct {
m MarshalFunc
um UnmarshalFunc
}
// init describes config format.
func init() {
formats["json"] = format{m: json.Marshal, um: json.Unmarshal}
formats["yaml"] = format{m: yaml.Marshal, um: yaml.Unmarshal}
formats["yml"] = format{m: yaml.Marshal, um: yaml.Unmarshal}
formats["toml"] = format{
m: func(v interface{}) ([]byte, error) {
b := bytes.Buffer{}
err := toml.NewEncoder(&b).Encode(v)
return b.Bytes(), err
},
um: toml.Unmarshal,
}
}
// Init sets application name.
func Init(application string) {
applicationName = application
}
// Load gets config file.
func Load(path string, v interface{}) error {
if applicationName == "" {
panic("store: application name not defined")
}
if format, ok := formats[extension(path)]; ok {
return LoadWith(path, v, format.um)
}
panic("store: unknown configuration format")
}
// Save stores file.
func Save(path string, v interface{}) error {
if applicationName == "" {
panic("store: application name not defined")
}
if format, ok := formats[extension(path)]; ok {
return SaveWith(path, v, format.m)
}
panic("store: unknown configuration format")
}
// LoadWith gets file.
func LoadWith(path string, v interface{}, um UnmarshalFunc) error {
if applicationName == "" {
panic("store: application name not defined")
}
globalPath := BuildPlatformPath(path)
data, err := ioutil.ReadFile(globalPath)
if err != nil {
return err
}
if err := um(data, v); err != nil {
return fmt.Errorf("store: failed to unmarshal %s: %v", path, err)
}
return nil
}
// SaveWith stores file.
func SaveWith(path string, v interface{}, m MarshalFunc) error {
if applicationName == "" {
panic("store: application name not defined")
}
var b bytes.Buffer
if data, err := m(v); err == nil {
b.Write(data)
} else {
return fmt.Errorf("store: failed to marshal %s: %v", path, err)
}
b.WriteRune('\n')
globalPath := BuildPlatformPath(path)
if err := os.MkdirAll(filepath.Dir(globalPath), os.ModePerm); err != nil {
return err
}
if err := ioutil.WriteFile(globalPath, b.Bytes(), os.ModePerm); err != nil {
return err
}
return nil
}
// extension returns filepath extension.
func extension(path string) string {
for i := len(path) - 1; i >= 0; i-- {
if path[i] == '.' {
return path[i+1:]
}
}
return ""
}
// BuildPlatformPath returns platform path.
func BuildPlatformPath(path string) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s\\%s\\%s", os.Getenv("APPDATA"), GetApplicationName(), path)
}
var unixConfigDir string
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
unixConfigDir = xdg
} else {
unixConfigDir = os.Getenv("HOME") + "/.config"
}
return fmt.Sprintf("%s/%s/%s", unixConfigDir, GetApplicationName(), path)
}
// GetApplicationName returns application name.
func GetApplicationName() string {
return applicationName
}
// GetAppConfig gets app config model.
func GetAppConfig() *model.AppConfig {
var appConfig model.AppConfig
err := Load("AppConfig.json", &appConfig)
if err != nil {
fmt.Printf("Error while loading app config file! %s", err.Error())
err = Save("AppConfig.json", &model.AppConfig{})
if err != nil {
fmt.Printf("Error while creating new app config file! %s", err.Error())
}
return &model.AppConfig{}
}
return &appConfig
}
// GetAppConfigData returns app config data.
func GetAppConfigData() (map[string]interface{}, error) {
var appConfig map[string]interface{}
err := Load("AppConfig.json", &appConfig)
if err != nil {
return map[string]interface{}{}, errors.New("unable to load app configuration")
}
return appConfig, nil
}
// GetAppConfigField returns app config field.
func GetAppConfigField(section string, field string) reflect.Value {
var appConfig model.AppConfig
_ = Load("AppConfig.json", &appConfig)
return reflect.Indirect(reflect.ValueOf(appConfig)).FieldByName(section).FieldByName(field)
}
// SaveAppConfig stores app config.
func SaveAppConfig(appConfig *model.AppConfig) error {
err := Save("AppConfig.json", &appConfig)
if err != nil {
return fmt.Errorf("Error while saving app config file! %s ", err.Error())
}
return nil
}
// GetRepositoriesConfig returns repositories config.
func GetRepositoriesConfig() []model.RepositoryConfig {
repositoriesConfig := make([]model.RepositoryConfig, 0)
err := Load("Repositories.json", &repositoriesConfig)
if err != nil {
fmt.Printf("Error while loading repositories config file! %s", err.Error())
err = Save("Repositories.json", []map[string]interface{}{})
if err != nil {
fmt.Printf("Error while creating new repositories config file! %s", err.Error())
}
return []model.RepositoryConfig{}
}
return repositoriesConfig
}
// GetRepositoriesConfigData returns repositories config data.
func GetRepositoriesConfigData() ([]map[string]interface{}, error) {
var repositoriesConfig []map[string]interface{}
err := Load("Repositories.json", &repositoriesConfig)
if err != nil {
return []map[string]interface{}{}, errors.New("unable to load repositories configuration")
}
return repositoriesConfig, nil
}
// GetActiveRepositoriesConfigData returns active repositories config.
func GetActiveRepositoriesConfigData() ([]map[string]interface{}, error) {
repositoriesConfig, _ := GetRepositoriesConfigData()
var activeRepositories []map[string]interface{}
for _, repo := range repositoriesConfig {
if repo["state"] == model.StateActive {
activeRepositories = append(activeRepositories, repo)
}
}
return activeRepositories, nil
}
// GetBlockedRepositoriesConfigData returns blocked repositories config.
func GetBlockedRepositoriesConfigData() ([]map[string]interface{}, error) {
repositoriesConfig, _ := GetRepositoriesConfigData()
var blockedRepositories []map[string]interface{}
for _, repo := range repositoriesConfig {
if repo["state"] == model.StateBlocked {
blockedRepositories = append(blockedRepositories, repo)
}
}
return blockedRepositories, nil
}
// GetRepositoryDestinationRepositoryName parses destination repository name.
func GetRepositoryDestinationRepositoryName(repoConfig *model.RepositoryConfig) string {
dpp := strings.Trim(strings.TrimRight(repoConfig.DestinationPlatformPath, "git"), ".")
return strings.Split(dpp, "/")[len(strings.Split(dpp, "/"))-1]
}
// GetRepositorySourceRepositoryName parses source repository name.
func GetRepositorySourceRepositoryName(repoConfig *model.RepositoryConfig) string {
spp := strings.Trim(strings.TrimRight(repoConfig.SourcePlatformPath, "git"), ".")
return strings.Split(spp, "/")[len(strings.Split(spp, "/"))-1]
}
// SaveRepositoriesConfig stores repositories config.
func SaveRepositoriesConfig(repositories []model.RepositoryConfig) error {
err := Save("Repositories.json", &repositories)
if err != nil {
return fmt.Errorf("Error while saving repositories config file! %s ", err.Error())
}
return nil
}
// GetPlatformsConfig gets platforms config.
func GetPlatformsConfig() []model.PlatformConfig {
platformsConfig := make([]model.PlatformConfig, 0)
err := Load("Platforms.json", &platformsConfig)
if err != nil {
fmt.Printf("Error while loading platforms config file! %s", err.Error())
err = Save("Platforms.json", []map[string]interface{}{})
if err != nil {
fmt.Printf("Error while creating new platforms config file! %s", err.Error())
}
return []model.PlatformConfig{}
}
return platformsConfig
}
// GetPlatformsConfigData returns platform config data.
func GetPlatformsConfigData() ([]map[string]interface{}, error) {
var platformsConfig []map[string]interface{}
err := Load("Platforms.json", &platformsConfig)
if err != nil {
return []map[string]interface{}{}, errors.New("unable to load platforms configuration")
}
return platformsConfig, nil
}
// SavePlatformsConfig stores platform config.
func SavePlatformsConfig(platforms []model.PlatformConfig) error {
err := Save("Platforms.json", &platforms)
if err != nil {
return fmt.Errorf("Error while saving platforms config file! %s ", err.Error())
}
return nil
}
// GetPlatformByUUID returns platform config by platform UUID.
func GetPlatformByUUID(UUID string) *model.PlatformConfig {
platformsList := GetPlatformsConfig()
for _, platform := range platformsList {
if platform.UUID == UUID {
return &platform
}
}
return nil
}
// GetRepositoryByUUID returns repository config by repository UUID.
func GetRepositoryByUUID(UUID string) *model.RepositoryConfig {
repositoriesList := GetRepositoriesConfig()
for _, repository := range repositoriesList {
if repository.UUID == UUID {
return &repository
}
}
return nil
}