Read the config with Viper and environment variables, on Go 1.27.4
The config loader uses Viper instead of BurntSushi/toml. Every setting can also come from UBERBRINGER_<SECTION>_<KEY>, which wins over the file, and UBERBRINGER_CONFIG sets the default of --config; set empty, no file is read, so a container can run from the environment alone. Each setting is bound to its own variable, so a variable named after a section changes nothing. The file is parsed with go-toml and checked as written: unknown keys, empty tables and keys that differ only in case are errors, a UTF-8 BOM is skipped and a UTF-16 file is refused. Values are decoded strictly: durations are strings, numbers decimal, flags true or false, and a fraction for a whole number is refused. Errors name the file, the line of a syntax error and the variables that were set, never their values. `uberbringer config get <key>` prints one setting as the daemon would use it. install.sh reads body_limit, bind_ip and port through it, so the config is checked before the service restarts and Caddy proxies to the configured address. The script then waits for the daemon to answer, downloads Caddy's key and source list before replacing them, requires Caddy's own repository, keeps the Caddyfile during dpkg upgrades, detects an existing import loosely, logs Caddy's other errors, drops query strings from the access log and validates the domain and printer address more tightly. go.mod names Go 1.27.4 with no toolchain line, and the workflows check against the go line. The changelog folds the unreleased entries into 0.2.0, which is not tagged yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92dcacb3b9
commit
eacd0bfd27
18 files changed
+1244
-248
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Value returns the setting at the dotted key, such as "webserver.port", in
|
||||
// the form a config file or an environment variable gives it: durations such
|
||||
// as "15s", lists comma-separated. Key names match case-insensitively.
|
||||
func (c *Config) Value(key string) (string, error) {
|
||||
v := reflect.ValueOf(*c)
|
||||
for part := range strings.SplitSeq(key, ".") {
|
||||
field, ok := fieldByKey(v, part)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unknown key %q", key)
|
||||
}
|
||||
v = field
|
||||
}
|
||||
if v.Kind() == reflect.Struct {
|
||||
return "", fmt.Errorf("%q is a section; name one of its settings", key)
|
||||
}
|
||||
switch value := v.Interface().(type) {
|
||||
case time.Duration:
|
||||
return value.String(), nil
|
||||
case []string:
|
||||
return strings.Join(value, ","), nil
|
||||
default:
|
||||
return fmt.Sprint(value), nil
|
||||
}
|
||||
}
|
||||
|
||||
// fieldByKey returns the field of the struct v whose mapstructure tag is
|
||||
// name.
|
||||
func fieldByKey(v reflect.Value, name string) (reflect.Value, bool) {
|
||||
if v.Kind() != reflect.Struct {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
t := v.Type()
|
||||
for i := range t.NumField() {
|
||||
if tag := t.Field(i).Tag.Get("mapstructure"); tag != "" && tag == strings.ToLower(name) {
|
||||
return v.Field(i), true
|
||||
}
|
||||
}
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
Reference in new issue
Block a user