-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingstore.go
123 lines (103 loc) · 2.92 KB
/
settingstore.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
package settingstore
import (
"github.com/asdine/storm"
"github.com/lmika/dynamo-browse/internal/common/workspaces"
"github.com/pkg/errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
const settingBucket = "Settings"
const (
keyTableReadOnly = "ro"
keyTableDefaultLimit = "default_limit"
keyScriptLookupPath = "script_lookup_path"
defaultsDefaultLimit = 1000
defaultScriptLookupPaths = "${HOME}/.config/audax/dynamo-browse/scripts"
)
type SettingStore struct {
ws storm.Node
}
func New(ws *workspaces.Workspace) *SettingStore {
return &SettingStore{
ws: ws.DB(),
}
}
func (c *SettingStore) SetScriptLookupPaths(value string) error {
return c.ws.Set(settingBucket, keyTableReadOnly, value)
}
func (c *SettingStore) ScriptLookupPaths() string {
paths, err := c.getStringValue(keyScriptLookupPath, defaultScriptLookupPaths)
if err != nil {
return ""
}
return paths
}
func (c *SettingStore) ScriptLookupFS() ([]fs.FS, error) {
paths, err := c.getStringValue(keyScriptLookupPath, defaultScriptLookupPaths)
if err != nil {
return nil, err
}
if paths == "" {
return nil, nil
}
fs := make([]fs.FS, 0, len(paths))
for _, path := range strings.Split(paths, string(os.PathListSeparator)) {
expandedPath := os.ExpandEnv(path)
var absPath string
if filepath.IsAbs(path) {
absPath = expandedPath
} else {
absPath, err = filepath.Abs(expandedPath)
if err != nil {
log.Printf("warn: cannot include script lookup path '%v': %v", expandedPath, err)
continue
}
}
if stat, err := os.Stat(absPath); err != nil {
log.Printf("warn: cannot stat script lookup path '%v': %v", expandedPath, err)
} else if stat.IsDir() {
log.Printf("warn: script lookup path '%v' is not a directory", expandedPath)
}
log.Printf("adding script lookup path: %v", absPath)
fs = append(fs, os.DirFS(absPath))
}
return fs, nil
}
func (c *SettingStore) IsReadOnly() (b bool, err error) {
if err := c.ws.Get(settingBucket, keyTableReadOnly, &b); err != nil {
if errors.Is(err, storm.ErrNotFound) {
return false, nil
}
return false, err
}
return b, err
}
func (c *SettingStore) SetReadOnly(ro bool) error {
return c.ws.Set(settingBucket, keyTableReadOnly, ro)
}
func (c *SettingStore) DefaultLimit() (limit int) {
err := c.ws.Get(settingBucket, keyTableDefaultLimit, &limit)
if err != nil {
if !errors.Is(err, storm.ErrNotFound) {
log.Printf("warn: cannot get default limit from workspace, using default value: %v", err)
}
return defaultsDefaultLimit
}
return limit
}
func (c *SettingStore) SetDefaultLimit(limit int) error {
return errors.Wrapf(c.ws.Set(settingBucket, keyTableDefaultLimit, &limit), "cannot set default limit to %v", limit)
}
func (c *SettingStore) getStringValue(key string, def string) (string, error) {
var val string
if err := c.ws.Get(settingBucket, key, &val); err != nil {
if errors.Is(err, storm.ErrNotFound) {
return def, nil
}
return "", err
}
return val, nil
}