-
Notifications
You must be signed in to change notification settings - Fork 256
/
config.go
52 lines (45 loc) · 1.25 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
package kv
import (
"context"
"fmt"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/kv/internal"
)
// Ensure configStore implements chronograf.ConfigStore.
var _ chronograf.ConfigStore = &configStore{}
// configID is the boltDB key where the configuration object is stored
var configID = []byte("config/v1")
// configStore uses bolt to store and retrieve global
// application configuration
type configStore struct {
client *Service
}
func (s *configStore) Get(ctx context.Context) (*chronograf.Config, error) {
var cfg chronograf.Config
err := s.client.kv.View(ctx, func(tx Tx) error {
v, err := tx.Bucket(configBucket).Get(configID)
if v == nil || err != nil {
cfg = chronograf.Config{
Auth: chronograf.AuthConfig{
SuperAdminNewUsers: false,
},
}
return nil
}
return internal.UnmarshalConfig(v, &cfg)
})
return &cfg, err
}
func (s *configStore) Update(ctx context.Context, cfg *chronograf.Config) error {
if cfg == nil {
return fmt.Errorf("config provided was nil")
}
return s.client.kv.Update(ctx, func(tx Tx) error {
if v, err := internal.MarshalConfig(cfg); err != nil {
return err
} else if err := tx.Bucket(configBucket).Put(configID, v); err != nil {
return err
}
return nil
})
}