-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
86 lines (75 loc) · 1.86 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
package main
import (
"os"
"log"
"reflect"
"gopkg.in/yaml.v3"
)
type Config struct {
Debug bool `default:"false"`
Daemon bool `default:"true"`
Server string
Binddn string
Password string
Base string
Update int `yaml:"update_interval" default:"600"`
Host bool `yaml:"host_specific_entries" default:"true"`
Restart bool `yaml:"restart_service_on_change" default:"true"`
Override string `yaml:"override_hostname"`
HostDN string
Local2LDAP bool `yaml:"sync_local_changes" default:"false"`
}
func ConfigInit() (Config, error) {
var conf string
home, err := os.UserHomeDir()
locations := []string{
home + "/.bureau.yaml",
home + "/.config/bureau/bureau.yaml",
"/etc/bureau/bureau.yaml",
"bureau.yaml",
}
for _, path := range locations {
if _, err = os.Stat(path); err == nil {
conf = path
break
}
}
data, err := os.ReadFile(conf)
if err != nil {
return Config{}, err
}
err = yaml.Unmarshal(data, &c)
if err != nil {
log.Fatalf("Error: %v", err)
} else if c.Debug {
for i := 0; i <= 9; i++ {
key := reflect.Indirect(reflect.ValueOf(c)).Type().Field(i).Name
value := reflect.ValueOf(c)
if key == "Password" && c.Password != "" {
log.Printf(" === Loading configuration %v: %s\n", key, "***HIDDEN PASSWORD***")
} else {
log.Printf(" === Loading configuration %v: %v\n", key, value.FieldByName(key))
}
}
}
// get hostname and set search base
host, err := os.Hostname()
if err != nil {
log.Fatalf("error: %v", err)
} else if c.Debug {
log.Printf(" === Getting hostname: %s", host)
}
if c.Host {
if c.Override != "" {
c.HostDN = "cn=" + c.Override + "," + c.Base
} else {
c.HostDN = "cn=" + host + "," + c.Base
}
} else {
c.HostDN = c.Base
}
if c.Debug {
log.Printf(" === Looking for files in: %s", c.HostDN)
}
return c, nil
}