-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.go
54 lines (44 loc) · 1.21 KB
/
file.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
// Forked from https://github.com/zouyx/agollo
package agollo
import (
"encoding/json"
"github.com/Shonminh/apollo-client/internal/apollo"
"github.com/Shonminh/apollo-client/internal/logger"
"github.com/pkg/errors"
"os"
)
type diskConfig struct {
*apollo.Config
}
// write config to file
func writeConfigFile(config *apollo.Config, configPath string) error {
if config == nil {
logger.LogError("apollo config is null can not write backup file")
return errors.New("apollo config is null can not write backup file")
}
file, e := os.Create(configPath)
if e != nil {
logger.LogError("writeConfigFile fail: %v", e)
return e
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", "\t")
dConfig := diskConfig{Config: config}
return encoder.Encode(dConfig)
}
// load config from file
func loadConfigFile(configPath string) (*apollo.Config, error) {
logger.LogInfo("load config file from: %v", configPath)
file, e := os.Open(configPath)
if e != nil {
return nil, errors.WithMessage(e, "os.Open")
}
defer file.Close()
dConfig := &diskConfig{}
e = json.NewDecoder(file).Decode(dConfig)
if e != nil {
return nil, errors.WithMessage(e, "json Decode")
}
return dConfig.Config, nil
}