/
config.go
74 lines (60 loc) · 1.58 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
package helpers
import (
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
type Config struct {
InfluxDB InfluxDB `yaml:"influxDB"`
TorontoWater TorontoWater `yaml:"torontoWater"`
SleepDuration int `yaml:"sleepDuration"`
LookDaysInPast int `yaml:"lookDaysInPast"`
}
type InfluxDB struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
Organization string `yaml:"organization"`
Bucket string `yaml:"bucket"`
}
type TorontoWater struct {
AccountNumber string `yaml:"accountNumber"`
LastName string `yaml:"lastName"`
PostalCode string `yaml:"postalCode"`
LastPaymentMethod string `yaml:"lastPaymentMethod"`
Mock bool `yaml:"mock"`
}
func ReadConfig(configFile string) Config {
var appConfig Config
// check if specific
if len(configFile) == 0 {
log.Fatalln("Configuration file not specified!")
}
// check file ending
if filepath.Ext(configFile) != ".yml" {
log.Fatalln("Configuration file is not YAML!")
}
// check if file exists
if !fileExists(configFile) {
log.Fatalln("Configuration file doesn't exist!")
}
// load file into config object
f, err := os.Open(configFile)
if err != nil {
log.Fatalln("Error reading the configuration file!")
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&appConfig)
if err != nil {
log.Fatalln("Error reading the configuration file! Is it valid YAML?")
}
return appConfig
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}