-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
73 lines (67 loc) · 1.62 KB
/
Copy pathconfig.go
File metadata and controls
73 lines (67 loc) · 1.62 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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
// View is a user-defined grouping of services. A view can match services by
// logon account (substring, case-insensitive), by an explicit list of service
// names, or both. An empty view matches everything.
type View struct {
ID string `json:"id"`
Name string `json:"name"`
Account string `json:"account"` // logon-account filter (substring)
Services []string `json:"services"` // explicit service names
}
// Config is the persisted application state.
type Config struct {
Views []View `json:"views"`
Favorites []string `json:"favorites"`
Theme string `json:"theme"`
}
func configPath() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", err
}
appDir := filepath.Join(dir, "KaizokuServiceManager")
if err := os.MkdirAll(appDir, 0o755); err != nil {
return "", err
}
return filepath.Join(appDir, "config.json"), nil
}
func loadConfig() (Config, error) {
cfg := Config{Views: []View{}, Favorites: []string{}}
path, err := configPath()
if err != nil {
return cfg, err
}
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
}
return cfg, err
}
if err := json.Unmarshal(data, &cfg); err != nil {
return cfg, err
}
if cfg.Views == nil {
cfg.Views = []View{}
}
if cfg.Favorites == nil {
cfg.Favorites = []string{}
}
return cfg, nil
}
func saveConfig(cfg Config) error {
path, err := configPath()
if err != nil {
return err
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0o644)
}