-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
88 lines (73 loc) · 2.37 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
package flex
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
// Config holds settings to be used by a client or daemon.
type Config struct {
// TestOption is used only for testing purposes.
TestOption string `yaml:"test-option,omitempty"`
// DefaultRemote holds the remote daemon name from the Remotes map
// that the client should communicate with by default.
// If empty it defaults to "local".
DefaultRemote string `yaml:"default-remote"`
// Remotes defines a map of remote daemon names to the details for
// communication with the named daemon.
// The implicit "local" remote is always available and communicates
// with the local daemon over a unix socket.
Remotes map[string]RemoteConfig `yaml:"remotes"`
// ListenAddr the defines an alternative address for the local daemon
// to listen on. If empty, the daemon will listen only on the local
// unix socket address.
ListenAddr string `yaml:"listen-addr"`
}
// RemoteConfig holds details for communication with a remote daemon.
type RemoteConfig struct {
Addr string `yaml:"addr"`
}
var configPath = "$HOME/.flex/config.yaml"
// LoadConfig reads the configuration from $HOME/.flex/config.yaml.
func LoadConfig() (*Config, error) {
data, err := ioutil.ReadFile(os.ExpandEnv(configPath))
if os.IsNotExist(err) {
// A missing file is equivalent to the default configuration.
return &Config{}, nil
}
if err != nil {
return nil, fmt.Errorf("cannot read config file: %v", err)
}
var c Config
err = yaml.Unmarshal(data, &c)
if err != nil {
return nil, fmt.Errorf("cannot parse configuration: %v", err)
}
return &c, nil
}
// SaveConfig writes the provided configuration to $HOME/.flex/config.yaml.
func SaveConfig(c *Config) error {
fname := os.ExpandEnv(configPath)
// Ignore errors on these two calls. Create will report any problems.
os.Remove(fname + ".new")
os.Mkdir(filepath.Dir(fname), 0700)
f, err := os.Create(fname + ".new")
if err != nil {
return fmt.Errorf("cannot create config file: %v", err)
}
// If there are any errors, do not leave it around.
defer f.Close()
defer os.Remove(fname + ".new")
data, err := yaml.Marshal(c)
_, err = f.Write(data)
if err != nil {
return fmt.Errorf("cannot write configuration: %v", err)
}
f.Close()
err = os.Rename(fname+".new", fname)
if err != nil {
return fmt.Errorf("cannot rename temporary config file: %v", err)
}
return nil
}