/
config.go
64 lines (52 loc) · 1.2 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
package gps2mqtt
import (
"fmt"
"os"
"time"
"github.com/BurntSushi/toml"
)
type Config struct {
md toml.MetaData `toml:"-"`
MQTT ConfigMQTT
Meta map[string]ConfigMeta
Protocols map[string]toml.Primitive `toml:"protocol"`
}
type ConfigMQTT struct {
ClientName string
Brokers []string
KeepAlive time.Duration
PingTimeout time.Duration
Username string
Password string
}
type ConfigMeta struct {
Name string
Icon string
}
func LoadConfiguration(file string) (*Config, error) {
config := Config{
MQTT: ConfigMQTT{
ClientName: "gps2mqtt",
KeepAlive: time.Minute,
PingTimeout: time.Second,
Username: os.Getenv("MQTT_USERNAME"),
Password: os.Getenv("MQTT_PASSWORD"),
},
}
var err error
if config.md, err = toml.DecodeFile(file, &config); err != nil {
return nil, err
}
return &config, nil
}
func (c *Config) ProtocolConfiguration(name string, v interface{}) (err error) {
if c.md.IsDefined("protocol", name) {
err = c.md.PrimitiveDecode(c.Protocols[name], v)
return
}
return fmt.Errorf("unable to find %s in configuration under protocol", name)
}
func (c *Config) Whitelist(name string) bool {
_, exists := c.Meta[name]
return exists
}