forked from devangel/config
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
175 lines (156 loc) · 3.57 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
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
// Cross-platform configuration manager
package config
import (
"fmt"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
)
type Config struct {
Base string
Entries map[string]ConfigEntry
}
type ConfigEntry struct {
Key string
Value string
}
// Create a new Config manager
func NewConfig(base string) (config *Config) {
config = &Config{}
config.Base = base
config.Entries = make(map[string]ConfigEntry)
return
}
// List keys for a given config
func (c *Config) List(name string) (keys []string, err error) {
configDir, err := c.configDirectory()
if err != nil {
return
}
dir := path.Join(configDir, name)
files, err := ioutil.ReadDir(dir)
if err != nil {
return
}
for _, file := range files {
keys = append(keys, file.Name())
}
sort.Strings(keys)
return
}
// Save a key/value pair for a config
func (c *Config) Save(name, key, value string) (err error) {
return c.SaveGlobal(name, key, value)
}
func (c *Config) SaveGlobal(name, key, value string) (err error) {
dir, err := c.configDirectory()
if err != nil {
return
}
filename := path.Join(dir, name, key)
err = c.writeFile(filename, value)
return
}
func (c *Config) SaveLocal(name, key, value string) (err error) {
dir, err := c.localConfigDirectory()
if err != nil {
return
}
filename := path.Join(dir, name, key)
err = c.writeFile(filename, value)
return
}
// Load a value for a config key
func (c *Config) Load(name, key string) (body string, err error) {
return c.LoadGlobal(name, key)
}
func (c *Config) LoadGlobal(name, key string) (body string, err error) {
dir, err := c.configDirectory()
if err != nil {
return
}
filename := path.Join(dir, name, key)
body, err = c.readFile(filename)
return
}
// Load value from local config directory if it exists; otherwise from global
// config directory
func (c *Config) LoadLocalOrGlobal(name, key string) (body string, err error) {
dir, err := c.localConfigDirectory()
if err != nil {
return
}
filename := path.Join(dir, name, key)
body, err = c.readFile(filename)
if err != nil {
return c.LoadGlobal(name, key)
}
return
}
// Delete a config key/value pair
func (c *Config) Delete(name, key string) (err error) {
return c.DeleteGlobal(name, key)
}
func (c *Config) DeleteGlobal(name, key string) (err error) {
dir, err := c.configDirectory()
if err != nil {
return
}
filename := path.Join(dir, name, key)
err = os.Remove(filename)
return
}
// Delete a config key/value pair
func (c *Config) DeleteLocalOrGlobal(name, key string) (err error) {
dir, err := c.localConfigDirectory()
if err != nil {
return
}
filename := path.Join(dir, name, key)
err = os.Remove(filename)
if err != nil {
return c.DeleteGlobal(name, key)
}
return
}
func (c *Config) configDirectory() (configDir string, err error) {
home, err := c.homeDirectory()
if err != nil {
return
}
configDir = path.Join(home, fmt.Sprintf(".%s", c.Base))
return
}
func (c *Config) localConfigDirectory() (configDir string, err error) {
cwd, err := os.Getwd()
if err != nil {
return
}
configDir = path.Join(cwd, fmt.Sprintf(".%s", c.Base))
return
}
func (c *Config) writeFile(filename, body string) (err error) {
dir := filepath.Dir(filename)
if err = os.MkdirAll(dir, 0700); err != nil {
return err
}
err = ioutil.WriteFile(filename, []byte(body), 0600)
if err != nil {
return
}
return
}
func (c *Config) readFile(filename string) (body string, err error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
body = string(data)
return
}
func (c *Config) homeDirectory() (string, error) {
return homedir.Dir()
}