-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
76 lines (64 loc) · 1.58 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
67
68
69
70
71
72
73
74
75
76
package core
import (
"encoding/json"
"os"
"path/filepath"
)
var configFilePath = filepath.Join(GetConfDir(), "config.json")
type Config struct {
ClientID string `json:"ClientID"`
ClientSecret string `json:"ClientSecret"`
BackupLinksSecret string `json:"BackupLinksSecret"`
DownloadDir string `json:"DownloadDir"`
MaxConcurrentDownloads uint `json:"MaxConcurrentDownloads"`
}
func (t *Kanna) loadConfiguration() error {
if err := os.MkdirAll(GetConfDir(), os.ModePerm); err != nil {
return err
}
t.Config = &Config{}
if confBytes, err := os.ReadFile(configFilePath); err == nil {
err = json.Unmarshal(confBytes, t.Config)
if err != nil {
return err
}
}
err := t.Config.sanitiseConfigurations()
if err != nil {
return err
}
return t.saveConfiguration()
}
func (t *Kanna) saveConfiguration() error {
confBytes, err := json.MarshalIndent(t.Config, "", "\t")
if err != nil {
return err
}
if err = os.MkdirAll(GetConfDir(), os.ModePerm); err != nil {
return err
}
return os.WriteFile(configFilePath, confBytes, os.ModePerm)
}
func (c *Config) sanitiseConfigurations() error {
if c.DownloadDir == "" {
downloadDir, err := os.UserHomeDir()
if err != nil {
return err
}
c.DownloadDir = filepath.Join(downloadDir, "Downloads")
}
if c.MaxConcurrentDownloads <= 0 {
c.MaxConcurrentDownloads = 2
}
return nil
}
func GetConfDir() string {
configDir, err := os.UserConfigDir()
if err != nil {
configDir, err = os.UserHomeDir()
if err != nil {
configDir = ""
}
}
return filepath.Join(configDir, "kanna")
}