-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
89 lines (78 loc) · 1.48 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
package main
import (
"fmt"
"log"
"os"
"time"
"gopkg.in/yaml.v2"
)
var (
Version string
Buildtime string
configFile string
)
type feed struct {
URL, Template string
Format string
Visibility string
Sensitive bool
ContentWarning string
}
type config struct {
Accounts []account
LastUpdated time.Time
HttpConfig httpConfig
Meta meta
}
type meta struct {
Name string
Version string
Buildtime string
}
type account struct {
AccessToken string
Name string
InstanceURL string
Feeds []feed
}
type httpConfig struct {
UserAgent string
}
func readConfig(fileName string) *config {
log.Println("reading config...")
configFile = fileName
config := new(config)
cf, err := os.ReadFile(configFile)
if err != nil {
log.Fatalln("Failed to read config: ", err)
}
err = yaml.Unmarshal(cf, &config)
if err != nil {
log.Panic(err)
}
if debug {
log.Printf("Config:\n\n%v", config)
}
config.Meta.Name = "gof"
config.Meta.Version = Version
config.Meta.Buildtime = Buildtime
config.HttpConfig.UserAgent = fmt.Sprintf("%s/%s",
config.Meta.Name, config.Meta.Version)
return config
}
func (cf *config) updateLastUpdated() {
log.Println("updating LastUpdated key...")
cf.LastUpdated = time.Now()
}
func (cf *config) Save() error {
log.Println("saving config to file...")
cfBytes, err := yaml.Marshal(cf)
if err != nil {
return err
}
err = os.WriteFile(configFile, cfBytes, 0644)
if err != nil {
return err
}
return nil
}