forked from APSL/k8s-database-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
58 lines (49 loc) · 1.11 KB
/
config.go
File metadata and controls
58 lines (49 loc) · 1.11 KB
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
// vim:set sw=8 ts=8 noet:
package main
import (
"fmt"
"errors"
"log"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type MySQLConfig struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Class string `yaml:"class"`
}
type PostgreSQLConfig struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Class string `yaml:"class"`
}
type DBConfig struct {
MySQL []MySQLConfig `yaml:"mysql"`
PostgreSQL []PostgreSQLConfig `yaml:"postgresql"`
}
func read_config(filename string) (*DBConfig, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
dbconfig := new(DBConfig)
err = yaml.Unmarshal([]byte(data), dbconfig)
if (err != nil) {
return nil, err
}
for _, mysql := range dbconfig.MySQL {
if mysql.Name == "" {
return nil, errors.New("MySQL server missing 'name'")
}
if mysql.URL == "" {
return nil, errors.New(fmt.Sprintf(`MySQL server "%s" missing URL`,
mysql.Name))
}
if mysql.Class == "" {
mysql.Class = "default"
log.Printf(`note: MySQL server "%s" missing class; set to "default"`,
mysql.Name)
}
}
return dbconfig, nil
}