-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
60 lines (54 loc) · 1.34 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
package main
import (
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/alexcoder04/friendly/v2/ffiles"
"gopkg.in/yaml.v3"
)
func GetSettingsFile() string {
confDir, err := ffiles.GetConfigDirFor(PROGRAM_NAME)
if err != nil {
log.Fatalln("Cannot get config directory")
}
return path.Join(confDir, "settings.yml")
}
func GetRecepientsFile() string {
confDir, err := ffiles.GetConfigDirFor(PROGRAM_NAME)
if err != nil {
log.Fatalln("Cannot get config directory")
}
return path.Join(confDir, "recipients.txt")
}
func ReadSettingsFromFile(file string) Settings {
log.Printf("Loading settings from %s...\n", file)
data, err := ioutil.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
log.Fatalf("Settings file (%s) does not exist\n", file)
}
log.Fatalln("Cannot open settings file")
}
settings := Settings{}
err = yaml.Unmarshal(data, &settings)
if err != nil {
log.Fatalln("Your settings file doesn't look right")
}
return settings
}
func ReadRecipientsAddressesFromFile(file string) []string {
lines, err := ffiles.ReadLines(file)
if err != nil {
if os.IsNotExist(err) {
log.Fatalf("Recipients file (%s) does not exist\n", file)
}
log.Fatalln("Cannot read recipients")
}
recipients := make([]string, 0)
for _, l := range lines {
recipients = append(recipients, strings.TrimSpace(l))
}
return recipients
}