-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
66 lines (51 loc) · 1.18 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
package noteofcli
import (
"errors"
"path/filepath"
"github.com/donatj/appsettings"
"github.com/shibukawa/configdir"
)
const keyToken = "token"
const editorToken = "editor"
type Config struct {
settings *appsettings.AppSettings
}
func NewConfig() (*Config, error) {
configDirs := configdir.New("donatstudios", "noteof-cli")
folders := configDirs.QueryFolders(configdir.Global)
cache := configDirs.QueryFolders(configdir.Cache)
if len(folders) < 1 || len(cache) < 1 {
return nil, errors.New("unable to store config")
}
folders[0].MkdirAll()
cache[0].MkdirAll()
s, err := appsettings.NewAppSettings(filepath.Join(folders[0].Path, "settings.json"))
if err != nil {
return nil, err
}
return &Config{
settings: s,
}, nil
}
func (c *Config) GetToken() string {
s, err := c.settings.GetString(keyToken)
if err != nil {
return ""
}
return s
}
func (c *Config) SetToken(s string) {
c.settings.SetString(keyToken, s)
c.settings.Persist()
}
func (c *Config) GetEditor() string {
s, err := c.settings.GetString(editorToken)
if err != nil {
return ""
}
return s
}
func (c *Config) SetEditor(s string) {
c.settings.SetString(editorToken, s)
c.settings.Persist()
}