-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
45 lines (37 loc) · 1.08 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
package database
import (
"github.com/icinga/icinga-go-library/config"
"github.com/pkg/errors"
)
// Config defines database client configuration.
type Config struct {
Type string `yaml:"type" default:"mysql"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Database string `yaml:"database"`
User string `yaml:"user"`
Password string `yaml:"password"`
TlsOptions config.TLS `yaml:",inline"`
Options Options `yaml:"options"`
}
// Validate checks constraints in the supplied database configuration and returns an error if they are violated.
func (c *Config) Validate() error {
switch c.Type {
case "mysql", "pgsql":
default:
return unknownDbType(c.Type)
}
if c.Host == "" {
return errors.New("database host missing")
}
if c.User == "" {
return errors.New("database user missing")
}
if c.Database == "" {
return errors.New("database name missing")
}
return c.Options.Validate()
}
func unknownDbType(t string) error {
return errors.Errorf(`unknown database type %q, must be one of: "mysql", "pgsql"`, t)
}