-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
177 lines (148 loc) · 4.05 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
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
package main
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"filippo.io/age"
"github.com/spf13/cast"
)
type DatabaseConfig struct {
Host string `conf:"DB_HOST"`
Port int `conf:"DB_PORT,5432"`
RootUsername string `conf:"DB_ROOT_USER,postgres"`
RootPassword string `conf:"DB_ROOT_PASSWORD"`
Username string `conf:"DB_USER_NAME"`
Password string `conf:"DB_USER_PASSWORD"`
Database string `conf:"DB_DATABASE"`
PgExtensions string `conf:"DB_PG_EXTENSIONS"`
}
type BackupConfig struct {
Database bool `conf:"BACKUP_DATABASE,false"`
DataDirectories string `conf:"BACKUP_DATA_DIR"`
DataDirectoriesExclude string `conf:"BACKUP_DATA_EXCLUDE"`
Schedule string `conf:"BACKUP_SCHEDULE,@daily"`
Storage string `conf:"BACKUP_STORAGE,/backup"`
AgeRecipients []*age.X25519Recipient `conf:"BACKUP_AGE_RECIPIENTS"`
AgePassword *age.ScryptRecipient `conf:"BACKUP_AGE_PASSWORD"`
RClonePath string `conf:"BACKUP_RCLONE_PATH"`
RCloneConfig string `conf:"BACKUP_RCLONE_CONFIG"`
}
func (c *BackupConfig) ageRecipients() []age.Recipient {
var recipients []age.Recipient
for _, recipient := range c.AgeRecipients {
recipients = append(recipients, recipient)
}
if c.AgePassword != nil {
recipients = append(recipients, c.AgePassword)
}
return recipients
}
type Config struct {
Database DatabaseConfig
Backup BackupConfig
}
// validate configuration
func (c *Config) validate() error {
db := c.Database
if db.Host != "" {
if db.Username == "" {
return errors.New("database host given but username is missing")
}
if db.Password == "" {
return errors.New("database host given but user password is missing")
}
if db.Database == "" {
return errors.New("database host given but database name is missing")
}
}
if c.Backup.Database && db.Host == "" {
return errors.New("database config missing for backup")
}
if c.Backup.AgeRecipients != nil && c.Backup.AgePassword != nil {
return errors.New("only age recipients OR a password is supported")
}
return nil
}
func loadStruct(st reflect.Value) error {
for i := 0; i < st.NumField(); i++ {
field := st.Field(i)
fieldType := st.Type().Field(i)
// load sub structures
if fieldType.Type.Kind() == reflect.Struct {
err := loadStruct(field)
if err != nil {
return err
}
continue
}
// get conf tag and skip this field if tag does not exist
tag, ok := fieldType.Tag.Lookup("conf")
if !ok {
continue
}
splitTag := strings.Split(tag, ",")
// check if default value exists
var defaultValue string
if len(splitTag) > 1 {
defaultValue = splitTag[1]
}
// get value from env
value, valueGiven := os.LookupEnv(splitTag[0])
// set value in struct
switch fieldType.Type.Kind() {
case reflect.String:
if valueGiven {
field.SetString(value)
} else {
field.SetString(defaultValue)
}
case reflect.Int:
if valueGiven {
field.SetInt(cast.ToInt64(value))
} else {
field.SetInt(cast.ToInt64(defaultValue))
}
case reflect.Bool:
if valueGiven {
field.SetBool(cast.ToBool(value))
} else {
field.SetBool(cast.ToBool(defaultValue))
}
case reflect.Slice:
if !valueGiven {
continue
}
if fieldType.Type.Elem() == reflect.TypeOf(new(age.X25519Recipient)) {
var recipients []*age.X25519Recipient
for _, key := range strings.Split(value, ",") {
recipient, err := age.ParseX25519Recipient(strings.TrimSpace(key))
if err != nil {
return fmt.Errorf("invalid recipient given %s: %w", key, err)
}
recipients = append(recipients, recipient)
}
field.Set(reflect.ValueOf(recipients))
} else {
panic("unsupported slice type")
}
case reflect.Ptr:
if !valueGiven {
continue
}
if fieldType.Type.Elem() == reflect.TypeOf(age.ScryptRecipient{}) {
recipient, err := age.NewScryptRecipient(value)
if err != nil {
return fmt.Errorf("invalid password given: %w", err)
}
field.Set(reflect.ValueOf(recipient))
} else {
panic("unsupported pointer type")
}
default:
panic("unsupported struct field type")
}
}
return nil
}