-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_config.go
78 lines (67 loc) · 1.9 KB
/
save_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
package autoloader
import (
"github.com/kyaxcorp/go-core/core/helpers/file"
// cassandraConfig "github.com/kyaxcorp/go-core/core/clients/db/driver/cassandra/config"
cfgData "github.com/kyaxcorp/go-core/core/config/data"
"github.com/kyaxcorp/go-core/core/helpers/errors2"
"github.com/kyaxcorp/go-core/core/helpers/hash"
//brokerConfig "github.com/kyaxcorp/go-core/core/services/broker/config"
"github.com/spf13/viper"
)
func SaveConfigFromMemory(cfg Config) error {
c := viper.New()
var _err error
// Setting the main config
c.Set("main", cfgData.MainConfig)
// Setting the custom config
c.Set("custom", cfg.CustomConfig)
// TODO: save config only by comparing if it's different!
// If it's diff, then overwrite it!
configPath := GetConfigFilePath()
if configPath == "" {
return errors2.New(0, "config path is empty")
}
configTmpPath := GetConfigTmpFilePath()
if configTmpPath == "" {
return errors2.New(0, "config tmp path is empty")
}
// Save the temporary config file
_err = c.WriteConfigAs(configTmpPath)
if _err != nil {
// log.Println("Failed to generate config!")
return _err
}
// Compare the 2 configs
tmpConfigHash, _err := hash.FileSha256(configTmpPath)
if _err != nil {
return _err
}
// Delete the tmp config
_, _err = file.Delete(configTmpPath)
if _err != nil {
return _err
}
// CHeck if config exists
isConfigExists := IsConfigExists()
if isConfigExists {
realConfigHash, _err := hash.FileSha256(configPath)
if _err != nil {
return _err
}
// log.Println(realConfigHash, tmpConfigHash)
// Compare the 2 configs
if tmpConfigHash == realConfigHash {
// It's the same configuration!
// log.Println("Same config!!! skipping save...")
return nil
}
}
// Save the real config file
_err = c.WriteConfigAs(configPath)
//_err = c.SafeWriteConfigAs(configPath)
if _err != nil {
// log.Println("Failed to generate config!")
return _err
}
return nil
}