forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
142 lines (125 loc) · 3.19 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mqtt
import (
"errors"
)
type Config struct {
// Enabled indicates whether the service should be enabled
Enabled bool `toml:"enabled" override:"enabled"`
Name string `toml:"name" override:"name"`
Default bool `toml:"default" override:"default"`
// URL of the MQTT Broker.
// Valid URLs include tcp://host:port, ws://host:port or ssl://host:port.
// If using ssl://host:port, one must also specify the SSL configuration options.
URL string `toml:"url" override:"url"`
// Path to CA file
SSLCA string `toml:"ssl-ca" override:"ssl-ca"`
// Path to host cert file
SSLCert string `toml:"ssl-cert" override:"ssl-cert"`
// Path to cert key file
SSLKey string `toml:"ssl-key" override:"ssl-key"`
// Use SSL but skip chain & host verification
InsecureSkipVerify bool `toml:"insecure-skip-verify" override:"insecure-skip-verify"`
// ClientID is the unique ID advertised to the MQTT broker from this client.
// Defaults to Name if empty.
ClientID string `toml:"client-id" override:"client-id"`
Username string `toml:"username" override:"username"`
Password string `toml:"password" override:"password,redact"`
// newClientF is a function that returns a client for a given config.
// It is used exclusively for testing.
newClientF func(c Config) (Client, error) `override:"-"`
}
// SetNewClientF sets the newClientF on a Config.
// It is used exclusively for testing.
func (c *Config) SetNewClientF(fn func(c Config) (Client, error)) {
c.newClientF = fn
}
func NewConfig() Config {
return Config{
Enabled: false,
Name: "default",
}
}
func (c Config) Validate() error {
if c.Name == "" {
return errors.New("must specify a name for the mqtt broker")
}
if c.Enabled {
if c.URL == "" {
return errors.New("must specify a url for mqtt service")
}
}
return nil
}
// NewClient creates a new client based off this configuration.
func (c Config) NewClient() (Client, error) {
newC := newClient
if c.newClientF != nil {
newC = c.newClientF
}
return newC(c)
}
func (c Config) Equal(o Config) bool {
if c.Enabled != o.Enabled {
return false
}
if c.Name != o.Name {
return false
}
if c.Default != o.Default {
return false
}
if c.URL != o.URL {
return false
}
if c.SSLCA != o.SSLCA {
return false
}
if c.SSLCert != o.SSLCert {
return false
}
if c.SSLKey != o.SSLKey {
return false
}
if c.InsecureSkipVerify != o.InsecureSkipVerify {
return false
}
if c.ClientID != o.ClientID {
return false
}
if c.Username != o.Username {
return false
}
if c.Password != o.Password {
return false
}
return true
}
type Configs []Config
// Validate calls config.Validate for each element in Configs
func (cs Configs) Validate() error {
defaultCount := 0
for _, c := range cs {
if c.Default {
defaultCount++
}
err := c.Validate()
if err != nil {
return err
}
}
if defaultCount > 1 {
return errors.New("more than one configuration is marked as the default")
}
if defaultCount == 0 && len(cs) > 1 {
return errors.New("no configuration is marked as the default")
}
return nil
}
// index generates a map of configs by name
func (cs Configs) index() map[string]Config {
m := make(map[string]Config, len(cs))
for _, c := range cs {
m[c.Name] = c
}
return m
}