forked from tucnak/store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
201 lines (164 loc) · 5.19 KB
/
store.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Package store is a dead simple configuration manager for Go applications.
package store
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime"
"github.com/BurntSushi/toml"
"github.com/go-yaml/yaml"
)
// MarshalFunc is any marshaler.
type MarshalFunc func(v interface{}) ([]byte, error)
// UnmarshalFunc is any unmarshaler.
type UnmarshalFunc func(data []byte, v interface{}) error
var (
applicationName = ""
formats = map[string]format{}
)
type format struct {
m MarshalFunc
um UnmarshalFunc
}
func init() {
formats["json"] = format{m: json.Marshal, um: json.Unmarshal}
formats["yaml"] = format{m: yaml.Marshal, um: yaml.Unmarshal}
formats["yml"] = format{m: yaml.Marshal, um: yaml.Unmarshal}
formats["toml"] = format{
m: func(v interface{}) ([]byte, error) {
b := bytes.Buffer{}
err := toml.NewEncoder(&b).Encode(v)
return b.Bytes(), err
},
um: toml.Unmarshal,
}
}
// Init sets up a unique application name that will be used for name of the
// configuration directory on the file system. By default, Store puts all the
// config data to to $XDG_CONFIG_HOME or $HOME on Linux systems
// and to %APPDATA% on Windows.
//
// Beware: Store will panic on any sensitive calls unless you run Init inb4.
func Init(application string) {
applicationName = application
}
// Register is the way you register configuration formats, by mapping some
// file name extension to corresponding marshal and unmarshal functions.
// Once registered, the format given would be compatible with Load and Save.
func Register(extension string, m MarshalFunc, um UnmarshalFunc) {
formats[extension] = format{m, um}
}
// Load reads a configuration from `path` and puts it into `v` pointer. Store
// supports either JSON, TOML or YAML and will deduce the file format out of
// the filename (.json/.toml/.yaml). For other formats of custom extensions
// please you LoadWith.
//
// Path is a full filename, including the file extension, e.g. "foobar.json".
// If `path` doesn't exist, Load will create one and emptify `v` pointer by
// replacing it with a newly created object, derived from type of `v`.
//
// Load panics on unknown configuration formats.
func Load(path string, v interface{}) error {
if applicationName == "" {
panic("store: application name not defined")
}
if format, ok := formats[extension(path)]; ok {
return LoadWith(path, v, format.um)
}
panic("store: unknown configuration format")
}
// Save puts a configuration from `v` pointer into a file `path`. Store
// supports either JSON, TOML or YAML and will deduce the file format out of
// the filename (.json/.toml/.yaml). For other formats of custom extensions
// please you LoadWith.
//
// Path is a full filename, including the file extension, e.g. "foobar.json".
//
// Save panics on unknown configuration formats.
func Save(path string, v interface{}) error {
if applicationName == "" {
panic("store: application name not defined")
}
if format, ok := formats[extension(path)]; ok {
return SaveWith(path, v, format.m)
}
panic("store: unknown configuration format")
}
// LoadWith loads the configuration using any unmarshaler at all.
func LoadWith(path string, v interface{}, um UnmarshalFunc) error {
if applicationName == "" {
panic("store: application name not defined")
}
globalPath := buildPlatformPath(path)
data, err := ioutil.ReadFile(globalPath)
if err != nil {
// There is a chance that file we are looking for
// just doesn't exist. In this case we are supposed
// to create an empty configuration file, based on v.
empty := reflect.New(reflect.TypeOf(v))
if innerErr := Save(path, &empty); innerErr != nil {
// Smth going on with the file system... returning error.
return err
}
v = empty
return nil
}
if err := um(data, v); err != nil {
return fmt.Errorf("store: failed to unmarshal %s: %v", path, err)
}
return nil
}
// SaveWith saves the configuration using any marshaler at all.
func SaveWith(path string, v interface{}, m MarshalFunc) error {
if applicationName == "" {
panic("store: application name not defined")
}
var b bytes.Buffer
if data, err := m(v); err == nil {
b.Write(data)
} else {
return fmt.Errorf("store: failed to marshal %s: %v", path, err)
}
b.WriteRune('\n')
globalPath := buildPlatformPath(path)
if err := os.MkdirAll(filepath.Dir(globalPath), os.ModePerm); err != nil {
return err
}
if err := ioutil.WriteFile(globalPath, b.Bytes(), os.ModePerm); err != nil {
return err
}
return nil
}
func extension(path string) string {
for i := len(path) - 1; i >= 0; i-- {
if path[i] == '.' {
return path[i+1:]
}
}
return ""
}
// buildPlatformPath builds a platform-dependent path for relative path given.
func buildPlatformPath(path string) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s\\%s\\%s", os.Getenv("APPDATA"),
applicationName,
path)
}
var unixConfigDir string
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
unixConfigDir = xdg
} else {
unixConfigDir = os.Getenv("HOME") + "/.config"
}
return fmt.Sprintf("%s/%s/%s", unixConfigDir,
applicationName,
path)
}
// SetApplicationName is DEPRECATED (use Init instead).
func SetApplicationName(handle string) {
applicationName = handle
}