This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
65 lines (52 loc) · 1.52 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
package main
import (
"errors"
"os"
"strings"
"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
)
var defaultConfigContent = `[settings]
viewer = vim
feed_directory = ~/rssnix
[feeds]`
type Configuration struct {
FeedDirectory string
Viewer string
Feeds []Feed
}
var Config Configuration
func LoadConfig() {
homePath, err := os.UserHomeDir()
if err != nil {
log.Error("Failed to get home path")
os.Exit(1)
}
if _, err := os.Stat(homePath + "/.config/rssnix/config.ini"); errors.Is(err, os.ErrNotExist) {
log.Warn("Config file does not exist, creating...")
os.MkdirAll(homePath+"/.config/rssnix", 0777)
file, err := os.Create(homePath + "/.config/rssnix/config.ini")
if err != nil {
log.Error("Failed to create a config file")
os.Exit(1)
}
defer file.Close()
_, err = file.WriteString(defaultConfigContent)
if err != nil {
log.Error("Failed to create a config file")
os.Exit(1)
}
log.Info("Config file created at " + homePath + "/.config/rssnix/config.ini")
}
cfg, err := ini.Load(homePath + "/.config/rssnix/config.ini")
Config = Configuration{}
Config.FeedDirectory = cfg.Section("settings").Key("feed_directory").String()
if strings.HasPrefix(Config.FeedDirectory, "~") {
Config.FeedDirectory = homePath + Config.FeedDirectory[1:]
}
os.MkdirAll(Config.FeedDirectory, 0777)
Config.Viewer = cfg.Section("settings").Key("viewer").String()
for _, key := range cfg.Section("feeds").Keys() {
Config.Feeds = append(Config.Feeds, Feed{key.Name(), key.String()})
}
}