-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
98 lines (82 loc) · 2.21 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"os"
"path/filepath"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
)
func NewConfig() *Config {
c := Config{}
c.readConfigFromFile()
c.HttpClient = newHttpClient()
if err := createDirIfNotExist(c.SaveLocation); err != nil {
logrus.Warn(err)
}
return &c
}
func newHttpClient() *retryablehttp.Client {
client := retryablehttp.NewClient()
client.RetryWaitMin = 5 * time.Second
client.RetryWaitMax = 60 * time.Second
client.RetryMax = 20
client.Logger = nil
return client
}
func (c *Config) LoadDefaults() {
homeDir := os.Getenv("HOME")
c.AutoFetchMeetingData = true
c.FetchOtherMedia = true
c.CreatePlaylist = true
c.SaveLocation = filepath.Join(homeDir, "Downloads/meetings")
c.Resolution = RES720
c.Language = "E"
c.PubSymbols = []string{"th", "bt"}
c.CacheLocation = filepath.Join(homeDir, "Downloads/meetings_cache")
}
func (c *Config) readConfigFromFile() {
homeDir := os.Getenv("HOME")
c.LoadDefaults()
data, err := os.ReadFile(filepath.Join(homeDir, CONFIG_FILE))
if err != nil {
c.writeConfigToFile()
}
err = toml.Unmarshal(data, c)
if err != nil {
logrus.Warn(err)
}
}
func (c *Config) writeConfigToFile() {
homeDir := os.Getenv("HOME")
logrus.Info("Saving settings")
config := struct {
AutoFetchMeetingData bool
FetchOtherMedia bool
CreatePlaylist bool
PurgeSaveDir bool
Resolution string
SaveLocation string
Language string
CacheLocation string
PubSymbols []string
}{
AutoFetchMeetingData: c.AutoFetchMeetingData,
FetchOtherMedia: c.FetchOtherMedia,
CreatePlaylist: c.CreatePlaylist,
PurgeSaveDir: c.PurgeSaveDir,
Resolution: c.Resolution,
SaveLocation: c.SaveLocation,
Language: c.Language,
PubSymbols: c.PubSymbols,
CacheLocation: c.CacheLocation,
}
configToml, err := toml.Marshal(config)
if err != nil {
logrus.Warn(err)
}
err = os.WriteFile(filepath.Join(homeDir, CONFIG_FILE), configToml, 0644)
if err != nil {
logrus.Warn(err)
}
}