-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
68 lines (56 loc) · 2.09 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
// Implements the reading and writing to and from a JSON config file.
package memfs
import (
"encoding/json"
"io/ioutil"
)
//===========================================================================
// Configuration Structs
//===========================================================================
// Replica implements the definition for a remote replica connections.
type Replica struct {
PID uint `json:"pid"` // Precedence ID for the replica
Name string `json:"name"` // Name of the replica
Host string `json:"host"` // IP address or hostname of the replica
Port int `json:"port"` // Port the replica is listening on
}
// Config implements the local configuration directives.
type Config struct {
Name string `json:"name"` // Identifier for replica lists
CacheSize uint64 `json:"cachesize"` // Maximum amount of memory used
Level string `json:"level"` // Minimum level to log at (debug, info, warn, error, critical)
ReadOnly bool `json:"readonly"` // Whether or not the FS is read only
Replicas []*Replica `json:"replicas"` // List of remote replicas in system
Path string `json:"-"` // Path the config was loaded from
}
//===========================================================================
// Config Methods
//===========================================================================
// Load a configuration from a path on disk by deserializing the JSON data.
func (conf *Config) Load(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// Unmarshal the JSON data
if err := json.Unmarshal(data, &conf); err != nil {
return err
}
// Save the loaded path
conf.Path = path
return nil
}
// Dump a configuration to JSON to the path on disk. If dump is an empty
// string then will dump the config to the path it was loaded from.
func (conf *Config) Dump(path string) error {
if path == "" {
path = conf.Path
}
// Marshal the JSON configuration data
data, err := json.Marshal(conf)
if err != nil {
return err
}
// Write the data to disk
return ioutil.WriteFile(path, data, 0644)
}