-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
240 lines (193 loc) · 5.85 KB
/
config.go
File metadata and controls
240 lines (193 loc) · 5.85 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2025 Nuclear Pasta. All rights reserved.
// Use of this source code is governed by the MIT license.
// license that can be found in the LICENSE file.
package confman
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type fileDefault struct {
f func(*Config, io.Writer) error
perm os.FileMode
}
// A Config represents the config of an application, with functions for convienently reading and writing various types of data to files in the configuration directory.
type Config struct {
defaults map[string]fileDefault
closers []io.Closer
path string
}
// OpenSpecific creates a Config pointing to the specified path.
func OpenSpecific(path string) (*Config, error) {
path = filepath.Clean(path)
if path == "" {
panic("path cannot be empty")
}
conf := &Config{
defaults: map[string]fileDefault{},
path: path,
}
err := conf.create()
if err != nil {
return nil, err
}
return conf, nil
}
// OpenHome creates a Config pointing to name beginning with a period inside of the system's home directory.
//
// Path contains the absolute path of the opened configuration directory.
func OpenHome(name string) (*Config, Path, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, "", err
}
path := filepath.Join(home, name)
path, err = filepath.Abs(path)
if err != nil {
return nil, "", err
}
conf, err := OpenSpecific(path)
if err != nil {
return nil, "", err
}
return conf, Path(path), nil
}
// Open creates a Config pointing to name inside of the system's default config directory.
//
// Path contains the absolute path of the opened configuration directory.
func Open(name string) (*Config, Path, error) {
configPath, dot, err := GetConfigPathForSystem()
if err != nil {
return nil, "", err
}
if _, err := os.Stat(configPath); err != nil {
if errors.Is(err, os.ErrNotExist) {
}
return nil, "", err
}
name = strings.TrimLeft(name, ".")
if dot {
name = "." + name
}
path := filepath.Join(configPath, name)
path, err = filepath.Abs(path)
if err != nil {
return nil, "", err
}
conf, err := OpenSpecific(path)
if err != nil {
return nil, "", err
}
return conf, Path(path), nil
}
// Closes all files created with [OpenReadAuto], [OpenWriteAuto], or [OpenCreateAuto].
//
// A list of errors from each close call is returned.
func (c *Config) Close() []error {
errors := []error{}
for _, closer := range c.closers {
if err := closer.Close(); err != nil {
errors = append(errors, err)
}
}
return errors
}
// Delete removes the directory pointed to by this Config along with all the data inside it.
//
// This is a very dangerous function, it can lead to unrecoverable data loss.
func (c *Config) Delete() error {
return os.RemoveAll(c.path)
}
func (c *Config) addCloser(closer io.Closer) {
c.closers = append(c.closers, closer)
}
func (c *Config) child(name string) string {
return filepath.Join(c.path, name)
}
// Stat returns a [FileInfo] describing the specified file.
// If there is an error, it will be of type [*PathError].
func (c *Config) Stat(name string) (os.FileInfo, error) {
return os.Stat(c.child(name))
}
// Exists checks if specified exists or not.
// Under normal circumstances, it should not return an error.
// If there is an error, it will be of type [*PathError].
func (c *Config) Exists(name string) (bool, error) {
if _, err := c.Stat(name); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
return true, nil
}
// DeleteFile removes the specified file or subdirectory from the configuration directory.
//
// This is a very dangerous function, it can lead to unrecoverable data loss.
func (c *Config) DeleteFile(name string) error {
return os.RemoveAll(c.path)
}
func (c *Config) create() error {
if exists, err := c.Exists(""); err != nil {
return err
} else if exists {
return nil
}
return os.Mkdir(c.path, os.ModeDir|0o777)
}
func (c *Config) verifyExists(name string) error {
exists, err := c.Exists(name)
if err != nil {
return err
} else if !exists {
if def, ok := c.defaults[name]; ok {
w, err := c.OpenCreate(name, def.perm)
if err != nil {
fmt.Println("err:", err.Error())
return err
}
return def.f(c, w)
}
return fmt.Errorf("'%s' does not exist", name)
}
return nil
}
func (c *Config) verifyNotExists(name string) error {
exists, err := c.Exists(name)
if err != nil {
return err
} else if exists {
return fmt.Errorf("'%s' already exists", name)
}
return nil
}
// DefaultFunc sets the callback for if the specified file doesn't exist when call any Config.Read, Config.Write function, or any of their variants.
func (c *Config) DefaultFunc(name string, perm os.FileMode, f func(*Config, io.Writer) error) {
if _, ok := c.defaults[name]; ok {
panic(fmt.Sprintf("'%s' already has a default", name))
}
c.defaults[name] = fileDefault{perm: perm, f: f}
}
// Default sets the bytes written to the specified file if it doesn't exist when call any Config.Read, Config.Write, or any of their variants.
//
// This is a convienence function over top of [DefaultFunc]
func (c *Config) Default(name string, perm os.FileMode, data []byte) {
c.DefaultFunc(name, perm, func(_ *Config, w io.Writer) error {
_, err := w.Write(data)
return err
})
}
// DefaultString sets the string written to the specified file if it doesn't exist when call any Config.Read, Config.Write, or any of their variants.
//
// This is a convienence function over top of [Default]
func (c *Config) DefaultString(name string, perm os.FileMode, str string) {
c.Default(name, perm, []byte(str))
}
// OpenRaw is the generalized open call; most users will use [OpenRead],
// [OpenWrite], or [OpenCreate] instead. It opens the named file with specified flag and file permissions.
func (c *Config) OpenRaw(name string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) {
return os.OpenFile(c.child(name), flag, perm)
}