-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
81 lines (70 loc) · 1.44 KB
/
storage.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
package main
import (
"encoding/json"
"log"
"os"
"path"
)
type Config struct {
Selected *Set
Downloaded map[string]string
APIKey string
UserId string
DownloadLocation string
APIEndpoint string
}
type writedConfig struct {
Selected []string
Downloaded map[string]string
APIKey string
UserId string
DownloadLocation string
APIEndpoint string
}
func getConfigFilePath() string {
configFolder, err := os.UserConfigDir()
checkError(err)
return path.Join(configFolder, "jellyfindl.json")
}
func writeConfig(conf Config) {
writedConf := writedConfig{
conf.Selected.Values(),
conf.Downloaded,
conf.APIKey,
conf.UserId,
conf.DownloadLocation,
conf.APIEndpoint,
}
b, err := json.Marshal(writedConf)
if err != nil {
panic(err)
}
os.WriteFile(getConfigFilePath(), b, os.ModePerm)
}
func getConfig() *Config {
b, err := os.ReadFile(getConfigFilePath())
conf := writedConfig{Selected: make([]string, 0)}
if err != nil {
if os.IsNotExist(err) {
b = []byte("{}")
os.WriteFile(getConfigFilePath(), b, os.ModePerm)
}
}
err = json.Unmarshal(b, &conf)
if err != nil {
log.Panic(err)
}
selected := NewSet()
selected.AddAll(conf.Selected)
if conf.Downloaded == nil {
conf.Downloaded = make(map[string]string)
}
return &Config{
selected,
conf.Downloaded,
conf.APIKey,
conf.UserId,
conf.DownloadLocation,
conf.APIEndpoint,
}
}