-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.go
More file actions
402 lines (350 loc) · 10.1 KB
/
configuration.go
File metadata and controls
402 lines (350 loc) · 10.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package dice
import (
"bufio"
"encoding/json"
"io"
"os"
"path"
"path/filepath"
"slices"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
// TODO: the configuration should not give paths to store files, but
// an FS based on the paths in the config!
type Settings interface {
// Base path storing DICE documents
RootFs() afero.Fs
// Directory storing signatures
SignaturesFs() afero.Fs
// Directory storing Modules
ModulesFs() afero.Fs
// Project directory, i.e., where to locate runtime file
ProjectFs() afero.Fs
// Workspace directory, i.e., where to output
WorkspaceFs() afero.Fs
Modules() string
Signatures() string
Workspace() string
Databases() string
}
// Standard paths to use to store DICE related data
// https://specifications.freedesktop.org/basedir-spec/latest/
type StandardPaths struct {
// Can be used to change the profile
// Default: "dice"
DICE_APPNAME string `json:"app_name,omitempty"`
// Path to state directory
// Default: "$XDG_STATE_HOME/$DICE_APPNAME" or "$HOME/.local/state/$DICE_APPNAME"
STATE_HOME string `json:"state,omitempty"`
// Path to data directory
// Default: "$XDG_DATA_HOME/$DICE_APPNAME" or "$HOME/.local/share/$DICE_APPNAME"
DATA_HOME string `json:"data,omitempty"`
// Path to configuration directory.
// Default: "$XDG_CONFIG_HOME/$DICE_APPNAME" or "$HOME/.config/$DICE_APPNAME"
CONFIG_HOME string `json:"config,omitempty"`
}
func (s *StandardPaths) UnmarshalJSON(data []byte) error {
type Aux StandardPaths
aux := &Aux{
DICE_APPNAME: "dice",
STATE_HOME: "-",
DATA_HOME: "-",
CONFIG_HOME: "-",
}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
*s = StandardPaths(*aux)
return nil
}
// This stdpaths overrides the values of the taken stdpaths if the values are unset.
// Unset values are "-"
func (s *StandardPaths) Overrides(stdpaths *StandardPaths) *StandardPaths {
if stdpaths.STATE_HOME == "-" {
stdpaths.STATE_HOME = s.STATE_HOME
}
if stdpaths.DATA_HOME == "-" {
stdpaths.DATA_HOME = s.DATA_HOME
}
if stdpaths.CONFIG_HOME == "-" {
stdpaths.CONFIG_HOME = s.CONFIG_HOME
}
return stdpaths
}
func DefaultPaths() *StandardPaths {
return BindStandardPaths(&StandardPaths{})
}
type stdpathsBuilder struct {
home string
app string
config string
state string
data string
}
func newStdpathsBuilder() *stdpathsBuilder {
return &stdpathsBuilder{home: os.Getenv("HOME")}
}
func (b *stdpathsBuilder) isValid(val string) bool {
return !slices.Contains([]string{"", "-"}, val)
}
func (b *stdpathsBuilder) bind(val, env, def string) string {
if b.isValid(val) {
return val
}
if v := os.Getenv(env); b.isValid(v) {
return v
}
return def
}
func (b *stdpathsBuilder) bindToApp(val, env, def string) string {
v := b.bind(val, env, def)
if v == val {
return val
}
return path.Join(v, b.app)
}
func (b *stdpathsBuilder) setApp(val string) *stdpathsBuilder {
b.app = b.bindToApp(val, "DICE_APPNAME", "dice")
return b
}
func (b *stdpathsBuilder) setConfig(val string) *stdpathsBuilder {
b.config = b.bindToApp(val, "XDG_CONFIG_HOME", path.Join(b.home, ".config"))
return b
}
func (b *stdpathsBuilder) setState(val string) *stdpathsBuilder {
b.state = b.bindToApp(val, "XDG_STATE_HOME", path.Join(b.home, ".local", "state"))
return b
}
func (b *stdpathsBuilder) setData(val string) *stdpathsBuilder {
b.data = b.bindToApp(val, "XDG_DATA_HOME", path.Join(b.home, ".local", "share"))
return b
}
func (b *stdpathsBuilder) build() *StandardPaths {
return &StandardPaths{
DICE_APPNAME: b.app,
CONFIG_HOME: b.config,
STATE_HOME: b.state,
DATA_HOME: b.data,
}
}
// Overrides empty standard paths. More of a purgue or clean job.
func BindStandardPaths(stdpaths *StandardPaths) *StandardPaths {
b := newStdpathsBuilder()
return b.setApp(stdpaths.DICE_APPNAME).
setConfig(stdpaths.CONFIG_HOME).
setData(stdpaths.DATA_HOME).
setState(stdpaths.STATE_HOME).
build()
}
type Configuration struct {
Paths *StandardPaths `json:"paths"`
Project *Project `json:"project"`
rFs afero.Fs
wFs afero.Fs
fs afero.Fs
study *Study
}
func (c *Configuration) SetWriteFs(fs afero.Fs) *Configuration {
c.wFs = fs
c.fs = afero.NewCopyOnWriteFs(c.rFs, c.wFs)
return c
}
// Returns the location where we store databases, modules, and signatures
func (c *Configuration) RootFs() afero.Fs {
return afero.NewBasePathFs(c.fs, c.Root())
}
func (c *Configuration) SignaturesFs() afero.Fs {
return afero.NewBasePathFs(c.RootFs(), "signatures")
}
func (c *Configuration) ModulesFs() afero.Fs {
return afero.NewBasePathFs(c.RootFs(), "modules")
}
func (c *Configuration) ProjectFs() afero.Fs {
if c.Project != nil {
return afero.NewBasePathFs(c.fs, c.Project.Path)
}
return c.RootFs()
}
// Returns the location where the current run will output data to
// If no project is set, return the data location
func (c *Configuration) WorkspaceFs() afero.Fs {
if c.study != nil {
return afero.NewBasePathFs(c.fs, c.study.Path)
}
return c.ProjectFs()
}
func (c *Configuration) DatabasesFs() afero.Fs {
return afero.NewBasePathFs(c.RootFs(), "databases")
}
func (c *Configuration) Root() string {
return c.Paths.DATA_HOME
}
func (c *Configuration) Modules() string {
return "modules"
}
func (c *Configuration) Signatures() string {
return "signatures"
}
func (c *Configuration) Workspace() string {
if c.study != nil {
return c.study.Path
}
return c.Root()
}
func (c *Configuration) Databases() string {
// NOTE: This is only useful for testing!
// Since gorm can only load gorm.Dialect stuff, we cannot pass
// a path to it. Best alternative is to create a temporary directory
// and delete it afterwards. BTW, on windows this behavior is different,
// so we cannot clear out the folder
switch c.wFs.(type) {
case *afero.MemMapFs:
dir, err := os.MkdirTemp(c.Paths.STATE_HOME, "databases-*")
if err != nil {
panic(err)
}
return dir
default:
return path.Join(c.Root(), "databases")
}
}
var (
ErrNotInDICEProject = errors.New("not inside a DICE project")
)
func findDICERoot(curr string) (string, error) {
dicePath := filepath.Join(curr, ".dice")
info, err := os.Stat(dicePath)
if err != nil && !os.IsNotExist(err) {
return "", err
}
// Is a DICE project and this is the location
// of the configuration folder
if info.IsDir() {
return dicePath, nil
}
parent := filepath.Dir(curr)
if parent == curr {
return "", ErrNotInDICEProject
}
return findDICERoot(parent)
}
// Attempts to find the root folder of '.dice' to find out whether
// we are in a DICE project and we can load the project configuration.
func projConf(stdpaths *StandardPaths) (*Configuration, error) {
var err error
curr, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "failed to get current directory")
}
root, err := findDICERoot(curr)
if err != nil {
return nil, err
}
return fpathConf(filepath.Join(root, "conf.json"), stdpaths)
}
// Reads a configuration from a file located in some path
// DICE configuration files should not reach more than 1MB right now, so
func fpathConf(p string, stdpaths *StandardPaths) (*Configuration, error) {
f, err := os.Open(p)
if err != nil {
return nil, errors.Wrap(err, "failed to open configuration file")
}
defer f.Close()
buf := make([]byte, 0, 1024)
r := bufio.NewReader(f)
if _, err := io.ReadFull(r, buf[:cap(buf)]); err != nil {
return nil, errors.Wrap(err, "failed to read configuration file")
}
var conf Configuration
if err := json.Unmarshal(buf, &conf); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal configuration")
}
stdpaths.Overrides(conf.Paths)
// override the configuration if it points to another file
// NOTE: I am not sure about this right now
// if conf.Paths.CONFIG_HOME != "" {
// oc, err := fpathConf(c.Paths.CONFIG_HOME)
// if err != nil {
// return nil, errors.Wrapf(err, "failed to load overriding configuration %s", c.Paths.CONFIG_HOME)
// }
// c.Override(oc)
// return c, nil
// }
return &conf, nil
}
func baseConf(paths *StandardPaths) *Configuration {
return &Configuration{
Paths: paths,
}
}
func pwdConf() (*Configuration, error) {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return &Configuration{
Paths: &StandardPaths{"dice", wd, wd, wd},
}, nil
}
// Load settings from a given path.
// If no path is given, look for a DICE config file locally;
// if this fails, look for a config file in the default config path;
// otherwise, return a default config.
// If the path is unset (i.e., "-"), then we use a "no-configuration"
// setting, where DICE will use the current directory as the main location
// for related files (e.g., modules, signatures, databases, etc.).
// If the path is another location, DICE will strictly look for a config file
// in that location and load it, returning an error if fails.
func LoadSettings(p string, stdpaths *StandardPaths, fs afero.Fs) (*Configuration, error) {
if fs == nil {
fs = afero.NewOsFs()
}
var (
conf *Configuration
err error
)
switch p {
// Default mode. No config given, check for the default config file path
// If no config is there, return default settings
case "":
// This config is the one of the project
// TODO: maybe improve this so we can understand when we are in a project
// before this happens, that way we can also load the project
conf, err = projConf(stdpaths)
if err != nil {
if errors.Is(err, ErrNotInDICEProject) {
goto CONFILE
}
return nil, errors.Wrap(err, "failed to load project settings")
}
break
CONFILE:
conf, err = fpathConf(filepath.Join(stdpaths.CONFIG_HOME, "conf.json"), stdpaths)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
goto CONFBASE
}
return nil, errors.Wrap(err, "failed to load settings from config directory")
}
break
CONFBASE:
conf = baseConf(stdpaths)
// Without settings, everything in the current path
case "-":
conf, err = pwdConf()
if err != nil {
return nil, errors.Wrap(err, "failed load without settings")
}
// Another path, check if there is a config file there
// If there isn't, return an error
default:
conf, err = fpathConf(p, stdpaths)
if err != nil {
return nil, errors.Wrap(err, "failed to load settings from location")
}
}
conf.rFs = afero.NewOsFs()
conf.SetWriteFs(fs)
return conf, nil
}