-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
cmd_gen.go
199 lines (163 loc) · 4.46 KB
/
cmd_gen.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"fmt"
"net/mail"
"net/url"
"os"
"reflect"
"regexp"
"strings"
"text/template"
"time"
"github.com/spf13/cobra"
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
// NewRunGenCmd implements the code generation cobra command.
func NewRunGenCmd() (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "gen",
RunE: runGenE,
}
return cmd
}
func runGenE(cmd *cobra.Command, args []string) (err error) {
if err = genConfigurationKeys(); err != nil {
return err
}
return nil
}
func genConfigurationKeys() (err error) {
data := loadKeysTemplate()
f, err := os.Create("./internal/configuration/schema/keys.go")
if err != nil {
return err
}
return keysTemplate.Execute(f, data)
}
var keysTemplate = template.Must(template.New("keys").Parse(`// Code generated by go generate. DO NOT EDIT.
//
// Run the following command to generate this file:
// go run ./cmd/authelia-scripts gen
//
package schema
// Keys represents the detected schema keys.
var Keys = []string{
{{- range .Keys }}
{{ printf "%q" . }},
{{- end }}
}
`))
type keysTemplateStruct struct {
Timestamp time.Time
Keys []string
}
func loadKeysTemplate() keysTemplateStruct {
config := schema.Configuration{
Storage: schema.StorageConfiguration{
Local: &schema.LocalStorageConfiguration{},
MySQL: &schema.MySQLStorageConfiguration{},
PostgreSQL: &schema.PostgreSQLStorageConfiguration{},
},
Notifier: schema.NotifierConfiguration{
FileSystem: &schema.FileSystemNotifierConfiguration{},
SMTP: &schema.SMTPNotifierConfiguration{
TLS: &schema.TLSConfig{},
},
},
AuthenticationBackend: schema.AuthenticationBackendConfiguration{
File: &schema.FileAuthenticationBackendConfiguration{
Password: &schema.PasswordConfiguration{},
},
LDAP: &schema.LDAPAuthenticationBackendConfiguration{
TLS: &schema.TLSConfig{},
},
},
Session: schema.SessionConfiguration{
Redis: &schema.RedisSessionConfiguration{
TLS: &schema.TLSConfig{},
HighAvailability: &schema.RedisHighAvailabilityConfiguration{},
},
},
IdentityProviders: schema.IdentityProvidersConfiguration{
OIDC: &schema.OpenIDConnectConfiguration{},
},
}
return keysTemplateStruct{
Timestamp: time.Now(),
Keys: readTags("", reflect.TypeOf(config)),
}
}
var decodedTypes = []reflect.Type{
reflect.TypeOf(mail.Address{}),
reflect.TypeOf(regexp.Regexp{}),
reflect.TypeOf(url.URL{}),
reflect.TypeOf(time.Duration(0)),
}
func containsType(needle reflect.Type, haystack []reflect.Type) (contains bool) {
for _, t := range haystack {
if needle.Kind() == reflect.Ptr {
if needle.Elem() == t {
return true
}
} else if needle == t {
return true
}
}
return false
}
func readTags(prefix string, t reflect.Type) (tags []string) {
tags = make([]string, 0)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("koanf")
if tag == "" {
tags = append(tags, prefix)
continue
}
switch field.Type.Kind() {
case reflect.Struct:
if !containsType(field.Type, decodedTypes) {
tags = append(tags, readTags(getKeyNameFromTagAndPrefix(prefix, tag, false), field.Type)...)
continue
}
case reflect.Slice:
if field.Type.Elem().Kind() == reflect.Struct {
if !containsType(field.Type.Elem(), decodedTypes) {
tags = append(tags, getKeyNameFromTagAndPrefix(prefix, tag, false))
tags = append(tags, readTags(getKeyNameFromTagAndPrefix(prefix, tag, true), field.Type.Elem())...)
continue
}
}
case reflect.Ptr:
switch field.Type.Elem().Kind() {
case reflect.Struct:
if !containsType(field.Type.Elem(), decodedTypes) {
tags = append(tags, readTags(getKeyNameFromTagAndPrefix(prefix, tag, false), field.Type.Elem())...)
continue
}
case reflect.Slice:
if field.Type.Elem().Elem().Kind() == reflect.Struct {
if !containsType(field.Type.Elem(), decodedTypes) {
tags = append(tags, readTags(getKeyNameFromTagAndPrefix(prefix, tag, true), field.Type.Elem())...)
continue
}
}
}
}
tags = append(tags, getKeyNameFromTagAndPrefix(prefix, tag, false))
}
return tags
}
func getKeyNameFromTagAndPrefix(prefix, name string, slice bool) string {
nameParts := strings.SplitN(name, ",", 2)
if prefix == "" {
return nameParts[0]
}
if len(nameParts) == 2 && nameParts[1] == "squash" {
return prefix
}
if slice {
return fmt.Sprintf("%s.%s[]", prefix, nameParts[0])
}
return fmt.Sprintf("%s.%s", prefix, nameParts[0])
}