This repository was archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
147 lines (112 loc) · 3.08 KB
/
Copy pathconfig.go
File metadata and controls
147 lines (112 loc) · 3.08 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
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
143
144
145
146
147
package config
import (
"errors"
"io"
"git.bode.fun/adsig/internal/util"
"gopkg.in/yaml.v3"
)
// Errors
// ------------------------------------------------------------------------
// TODO: Make a custom error type to wrap in.
var (
ErrDecode = errors.New("config: Can not content of decode config")
ErrNoContent = errors.New("config: The content file holds no content")
)
// Type Definition
// ------------------------------------------------------------------------
type configMember interface {
setDefaults()
normalize()
}
// Connection
// ------------------------------------------------------------------------
var _ configMember = (*Connection)(nil)
type Connection struct {
Address string `yaml:"address"`
UserDN string `yaml:"userDN"` //nolint
Password string `yaml:"password"` // TODO: Make this optional and introduce enum.
}
func (c *Connection) setDefaults() {}
func (c *Connection) normalize() {}
// Group
// ------------------------------------------------------------------------
var _ configMember = (*Group)(nil)
type Group struct {
// Optional
AdFilter string `yaml:"adFilter,omitempty"`
BaseDN string `yaml:"baseDN"` //nolint
// Optional
ExcludeEmails []string `yaml:"excludeEmails,omitempty"`
Templates []string
}
func (g *Group) setDefaults() {
if g.AdFilter == "" {
g.AdFilter = "(&(objectclass=person)(mail=*))"
}
}
func (g *Group) normalize() {
for i, email := range g.ExcludeEmails {
g.ExcludeEmails[i] = util.NormalizeEmail(email)
}
}
// Template
// ------------------------------------------------------------------------
var _ configMember = (*Template)(nil)
type Template struct {
Fields map[string]string `yaml:"fields"`
}
func (t *Template) setDefaults() {}
func (t *Template) normalize() {}
// Config
// ------------------------------------------------------------------------
var _ configMember = (*Config)(nil)
type Config struct {
Connection Connection `yaml:"connection"`
Groups map[string]Group `yaml:"groups"`
Templates map[string]Template `yaml:"templates"`
}
func (c *Config) setDefaults() {
c.Connection.setDefaults()
for gName, g := range c.Groups {
g.setDefaults()
c.Groups[gName] = g
}
for tName, t := range c.Templates {
t.setDefaults()
c.Templates[tName] = t
}
}
func (c *Config) normalize() {
c.Connection.normalize()
for gName, g := range c.Groups {
g.normalize()
c.Groups[gName] = g
}
for tName, t := range c.Templates {
t.normalize()
c.Templates[tName] = t
}
}
// Public Functions
// ------------------------------------------------------------------------
// TODO: Validate.
func FromYAML(r io.Reader) (Config, error) {
var cnf Config
decoder := yaml.NewDecoder(r)
decoder.KnownFields(true)
// err can be io.EOF or yaml.TypeError
if err := decoder.Decode(&cnf); err != nil {
var yamlTypeErr yaml.TypeError
if errors.Is(err, &yamlTypeErr) {
// TODO: Wrap the error.
return Config{}, ErrDecode
} else if errors.Is(err, io.EOF) {
return Config{}, ErrNoContent
} else {
return Config{}, ErrDecode
}
}
cnf.setDefaults()
cnf.normalize()
return cnf, nil
}