-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
77 lines (64 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
package main
import (
"errors"
"os"
"path/filepath"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)
const (
CONFIG_FILE = "mqtt4rclone.yml"
CONFIG_DIR = ".config"
CONFIG_ROOT = "/config"
)
type Mqtt struct {
Url string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Qos int `yaml:"qos"`
}
type Rclone struct {
ResponseTopic string `yaml:"response_topic"`
}
type Config struct {
Mqtt Mqtt `yaml:"mqtt"`
Rclone Rclone `yaml:"rclone"`
}
func getConfig() Config {
var config Config
configFile := filepath.Join(CONFIG_ROOT, CONFIG_FILE)
msg := configFile
data, err := os.ReadFile(configFile)
if err != nil {
homedir, _ := os.UserHomeDir()
configFile := filepath.Join(homedir, CONFIG_DIR, CONFIG_FILE)
msg += ", " + configFile
data, err = os.ReadFile(configFile)
}
if err != nil {
workingdir, _ := os.Getwd()
configFile := filepath.Join(workingdir, CONFIG_FILE)
msg += ", " + configFile
data, err = os.ReadFile(configFile)
}
if err != nil {
msg = "Configuration file could not be found: " + msg
log.Fatal().Msg(msg)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatal().Err(err).Msg("unmarshal")
}
err = validate(config)
if err != nil {
log.Fatal().Err(err).Msg("validate")
}
log.Trace().Msgf("Config: %+v\n", config)
return config
}
func validate(config Config) error {
if config.Mqtt.Url == "" {
return errors.New("Config error: MQTT Server URL is mandatory")
}
return nil
}