From 736e5895ce481c6fd2f51e65173fb63c2d330781 Mon Sep 17 00:00:00 2001 From: Didier Roche Date: Wed, 4 Oct 2023 15:53:18 +0200 Subject: [PATCH] Allow env variable override, without associated flags Visit manually env to bind every possibly related environment variable to be able to unmarshall those into a struct. More context on https://github.com/spf13/viper/pull/1429. --- cmd/authd/daemon/config.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/authd/daemon/config.go b/cmd/authd/daemon/config.go index 04cfcc9d1..92a2d4c02 100644 --- a/cmd/authd/daemon/config.go +++ b/cmd/authd/daemon/config.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -59,6 +60,20 @@ func initViperConfig(name string, cmd *cobra.Command, vip *viper.Viper) (err err vip.SetEnvPrefix(name) vip.AutomaticEnv() + // Visit manually env to bind every possibly related environment variable to be able to unmarshall + // those into a struct. + // More context on https://github.com/spf13/viper/pull/1429. + prefix := strings.ToUpper(name) + "_" + for _, e := range os.Environ() { + if !strings.HasPrefix(e, prefix) { + continue + } + + s := strings.Split(e, "=") + k := strings.ReplaceAll(strings.TrimPrefix(s[0], prefix), "_", ".") + vip.BindEnv(k, s[0]) + } + return nil }