-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
74 lines (64 loc) · 1.79 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
package encrepo
import (
"context"
"encoding/json"
"github.com/ipfs/go-datastore"
config "github.com/ipfs/kubo/config"
"github.com/pkg/errors"
)
const configKey = "config"
func isConfigInitialized(ctx context.Context, ds datastore.Datastore) bool {
has, err := ds.Has(ctx, datastore.NewKey(configKey))
if err != nil {
return false
}
return has
}
func initConfig(ctx context.Context, ds datastore.Datastore, conf *config.Config) error {
if isConfigInitialized(ctx, ds) {
return nil
}
// initialization is the one time when it's okay to write to the config
// without reading the config from disk and merging any user-provided keys
// that may exist.
if err := writeConfigToDatastore(ctx, ds, conf); err != nil {
return err
}
return nil
}
func readConfigFromDatastore(ctx context.Context, ds datastore.Datastore, dest interface{}) error {
confBytes, err := ds.Get(ctx, datastore.NewKey(configKey))
switch err {
case nil:
if err := json.Unmarshal(confBytes, dest); err != nil {
return errors.Wrap(err, "unmarshal config")
}
return nil
case datastore.ErrNotFound:
return datastore.ErrNotFound
default:
return errors.Wrap(err, "get config")
}
}
func writeConfigToDatastore(ctx context.Context, ds datastore.Datastore, src interface{}) error {
confBytes, err := config.Marshal(src)
if err != nil {
return errors.Wrap(err, "marshal config")
}
if err := ds.Put(ctx, datastore.NewKey(configKey), confBytes); err != nil {
return errors.Wrap(err, "put config in ds")
}
return nil
}
func getConfigFromDatastore(ctx context.Context, ds datastore.Datastore) (*config.Config, error) {
var conf config.Config
err := readConfigFromDatastore(ctx, ds, &conf)
switch err {
case nil:
return &conf, nil
case datastore.ErrNotFound:
return nil, nil
default:
return nil, err
}
}