-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
68 lines (56 loc) · 1.22 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// GotifyConfig -
type GotifyConfig struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
MinPriority int `yaml:"minPriority"`
}
// LogConfig -
type LogConfig struct {
LogLevel string `yaml:"logLevel"`
}
// Config -
type Config struct {
Gotify GotifyConfig `yaml:"gotify"`
Log LogConfig `yaml:"log"`
}
func (lc LogConfig) get() log.Level {
lvl, err := log.ParseLevel(lc.LogLevel)
if err != nil {
log.Warnf("cannot parse log level '%s': %s", lc.LogLevel, err.Error())
return log.InfoLevel
}
return lvl
}
func loadConfig() (c Config, err error) {
var confDir = os.Getenv("XDG_CONFIG_HOME")
if confDir == "" {
confDir = filepath.Join(os.Getenv("HOME"), ".config")
}
confDir = filepath.Join(confDir, "gotify-notify")
var confPath = filepath.Join(confDir, "config.yaml")
info, err := os.Stat(confPath)
if err != nil {
return
}
if info.IsDir() {
return c, fmt.Errorf("config file path '%s' is directory", confPath)
}
data, err := ioutil.ReadFile(confPath)
if err != nil {
return
}
err = yaml.Unmarshal([]byte(data), &c)
if err != nil {
return
}
return
}