-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
111 lines (94 loc) · 2.82 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
package main
import (
"errors"
"os"
"reflect"
"strconv"
"github.com/BurntSushi/toml"
)
type dbBackendEnum uint8
const (
// DBEnumMongo reporesents mongodb backend
DBEnumMongo dbBackendEnum = iota
)
var dcfg DaemonConfig
func (b *dbBackendEnum) UnmarshalText(text []byte) error {
s := string(text)
switch s {
case `mongo`, `mongodb`:
*b = DBEnumMongo
default:
return errors.New("Invalid value to database backend")
}
return nil
}
// A DaemonConfig represents configurations for tunasccount daemon
type DaemonConfig struct {
ReadOnly bool `toml:"read_only"`
DB DatabaseConfig `toml:"database"`
LDAP LDAPConfig `toml:"ldap"`
HTTP HTTPConfig `toml:"http"`
TUNA TUNAConfig `toml:"tunaccount"`
}
// A DatabaseConfig is the database config for tunaccount daemon
type DatabaseConfig struct {
Backend dbBackendEnum `toml:"backend"`
Addr string `toml:"addr" default:"127.0.0.1"`
Port int `toml:"port" default:"27017"`
Addrs []string `toml:"addrs"` // addr and port can be overrided by addrs
Name string `toml:"name" default:"tunaccount"`
User string `toml:"user"`
Password string `toml:"password"`
Options map[string]string `toml:"options"` // database specific options
}
// An LDAPConfig is ldap server configs
type LDAPConfig struct {
ListenAddr string `toml:"listen_addr" default:"127.0.0.1"`
ListenPort int `toml:"listen_port" default:"389"`
Suffix string `toml:"suffix"` // o=tuna
}
// An HTTPConfig is http server configs
type HTTPConfig struct {
ListenAddr string `toml:"listen_addr" default:"127.0.0.1"`
ListenPort int `toml:"listen_port" default:"9501"`
SecretKey string `toml:"secret_key"`
}
// A TUNAConfig specifies application level configs
type TUNAConfig struct {
MinimumUID int `toml:"minimum_uid" default:"2000"`
MinimumGID int `toml:"minimum_gid" default:"2000"`
DefaultGID int `toml:"default_gid" default:"2000"`
}
// A ClientConfig specifies configurations for tunaccount cli client
type ClientConfig struct {
ServerURL string `toml:"server_url"`
}
func setDefaultValues(v reflect.Value) {
for i := 0; i < v.NumField(); i++ {
vf := v.Field(i)
if vf.Kind() == reflect.Struct {
setDefaultValues(vf)
continue
}
dv := v.Type().Field(i).Tag.Get("default")
if dv != "" && vf.CanSet() {
switch vf.Kind() {
case reflect.String:
vf.SetString(dv)
case reflect.Int:
iv, _ := strconv.Atoi(dv)
vf.SetInt(int64(iv))
}
}
}
}
func loadDaemonConfig(cfgFile string) (*DaemonConfig, error) {
setDefaultValues(reflect.ValueOf(&dcfg).Elem())
if _, err := os.Stat(cfgFile); err == nil {
if _, err := toml.DecodeFile(cfgFile, &dcfg); err != nil {
logger.Errorf("Error parsing config file: %s", err.Error())
return nil, err
}
}
return &dcfg, nil
}