From 47593928f9afe6456c53490fd1f1d6c9331730b4 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 29 May 2023 20:43:06 +0000 Subject: [PATCH] fix(settings): use qdm12/gosettings `env.Get` --- go.mod | 2 +- go.sum | 6 ++--- internal/configuration/sources/env/health.go | 3 ++- internal/configuration/sources/env/helpers.go | 27 +++++++------------ internal/configuration/sources/env/log.go | 3 ++- internal/configuration/sources/env/openvpn.go | 11 ++++---- .../sources/env/openvpnselection.go | 3 ++- internal/configuration/sources/env/pprof.go | 3 ++- .../configuration/sources/env/provider.go | 3 ++- .../configuration/sources/env/publicip.go | 3 ++- internal/configuration/sources/env/reader.go | 5 ++-- internal/configuration/sources/env/server.go | 3 ++- .../sources/env/serverselection.go | 3 ++- internal/configuration/sources/env/system.go | 3 ++- internal/configuration/sources/env/updater.go | 3 ++- internal/configuration/sources/env/version.go | 3 ++- internal/configuration/sources/env/vpn.go | 3 ++- .../configuration/sources/env/wireguard.go | 4 +-- .../sources/env/wireguardselection.go | 3 ++- .../configuration/sources/secrets/helpers.go | 15 ++--------- 20 files changed, 51 insertions(+), 58 deletions(-) diff --git a/go.mod b/go.mod index 14a4af47..969a8468 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/golang/mock v1.6.0 github.com/qdm12/dns v1.11.0 github.com/qdm12/golibs v0.0.0-20210822203818-5c568b0777b6 - github.com/qdm12/gosettings v0.3.0-rc4 + github.com/qdm12/gosettings v0.3.0-rc7 github.com/qdm12/goshutdown v0.3.0 github.com/qdm12/gosplash v0.1.0 github.com/qdm12/gotree v0.2.0 diff --git a/go.sum b/go.sum index 44ba18d8..eb579c1d 100644 --- a/go.sum +++ b/go.sum @@ -91,10 +91,8 @@ github.com/qdm12/golibs v0.0.0-20210603202746-e5494e9c2ebb/go.mod h1:15RBzkun0i8 github.com/qdm12/golibs v0.0.0-20210723175634-a75ca7fd74c2/go.mod h1:6aRbg4Z/bTbm9JfxsGXfWKHi7zsOvPfUTK1S5HuAFKg= github.com/qdm12/golibs v0.0.0-20210822203818-5c568b0777b6 h1:bge5AL7cjHJMPz+5IOz5yF01q/l8No6+lIEBieA8gMg= github.com/qdm12/golibs v0.0.0-20210822203818-5c568b0777b6/go.mod h1:6aRbg4Z/bTbm9JfxsGXfWKHi7zsOvPfUTK1S5HuAFKg= -github.com/qdm12/gosettings v0.3.0-rc3 h1:ySstdo7i3h5z6iJ6R96YykC+JuluaVqncHxsFzndyUI= -github.com/qdm12/gosettings v0.3.0-rc3/go.mod h1:+hHzN8lsE63T01t6SruGzc6xkpvfsZFod/ooDs8FWnQ= -github.com/qdm12/gosettings v0.3.0-rc4 h1:rk0IG6mEs85zV2NzEH/XRbZx4FGGYpmcpGysFjEYL50= -github.com/qdm12/gosettings v0.3.0-rc4/go.mod h1:+hHzN8lsE63T01t6SruGzc6xkpvfsZFod/ooDs8FWnQ= +github.com/qdm12/gosettings v0.3.0-rc7 h1:fRIJe+pUIe2uzKjqtNnWIUsZsvpoPbXv5a/xo5utvbs= +github.com/qdm12/gosettings v0.3.0-rc7/go.mod h1:+hHzN8lsE63T01t6SruGzc6xkpvfsZFod/ooDs8FWnQ= github.com/qdm12/goshutdown v0.3.0 h1:pqBpJkdwlZlfTEx4QHtS8u8CXx6pG0fVo6S1N0MpSEM= github.com/qdm12/goshutdown v0.3.0/go.mod h1:EqZ46No00kCTZ5qzdd3qIzY6ayhMt24QI8Mh8LVQYmM= github.com/qdm12/gosplash v0.1.0 h1:Sfl+zIjFZFP7b0iqf2l5UkmEY97XBnaKkH3FNY6Gf7g= diff --git a/internal/configuration/sources/env/health.go b/internal/configuration/sources/env/health.go index a7d28762..bcae65df 100644 --- a/internal/configuration/sources/env/health.go +++ b/internal/configuration/sources/env/health.go @@ -5,10 +5,11 @@ import ( "time" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) func (s *Source) ReadHealth() (health settings.Health, err error) { - health.ServerAddress = getCleanedEnv("HEALTH_SERVER_ADDRESS") + health.ServerAddress = env.Get("HEALTH_SERVER_ADDRESS") _, health.TargetAddress = s.getEnvWithRetro("HEALTH_TARGET_ADDRESS", "HEALTH_ADDRESS_TO_PING") successWaitPtr, err := envToDurationPtr("HEALTH_SUCCESS_WAIT_DURATION") diff --git a/internal/configuration/sources/env/helpers.go b/internal/configuration/sources/env/helpers.go index 102bc10a..8c0d18f0 100644 --- a/internal/configuration/sources/env/helpers.go +++ b/internal/configuration/sources/env/helpers.go @@ -7,22 +7,13 @@ import ( "strings" "time" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/govalid/binary" "github.com/qdm12/govalid/integer" ) -// getCleanedEnv returns an environment variable value with -// surrounding spaces and trailing new line characters removed. -func getCleanedEnv(envKey string) (value string) { - value = os.Getenv(envKey) - value = strings.TrimSpace(value) - value = strings.TrimSuffix(value, "\r\n") - value = strings.TrimSuffix(value, "\n") - return value -} - func envToCSV(envKey string) (values []string) { - csv := getCleanedEnv(envKey) + csv := env.Get(envKey) if csv == "" { return nil } @@ -30,7 +21,7 @@ func envToCSV(envKey string) (values []string) { } func envToFloat64(envKey string) (f float64, err error) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) if s == "" { return 0, nil } @@ -39,7 +30,7 @@ func envToFloat64(envKey string) (f float64, err error) { } func envToStringPtr(envKey string) (stringPtr *string) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) if s == "" { return nil } @@ -47,7 +38,7 @@ func envToStringPtr(envKey string) (stringPtr *string) { } func envToBoolPtr(envKey string) (boolPtr *bool, err error) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) value, err := binary.Validate(s) if err != nil { return nil, err @@ -56,7 +47,7 @@ func envToBoolPtr(envKey string) (boolPtr *bool, err error) { } func envToIntPtr(envKey string) (intPtr *int, err error) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) if s == "" { return nil, nil //nolint:nilnil } @@ -68,7 +59,7 @@ func envToIntPtr(envKey string) (intPtr *int, err error) { } func envToUint8Ptr(envKey string) (uint8Ptr *uint8, err error) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) if s == "" { return nil, nil //nolint:nilnil } @@ -85,7 +76,7 @@ func envToUint8Ptr(envKey string) (uint8Ptr *uint8, err error) { } func envToUint16Ptr(envKey string) (uint16Ptr *uint16, err error) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) if s == "" { return nil, nil //nolint:nilnil } @@ -102,7 +93,7 @@ func envToUint16Ptr(envKey string) (uint16Ptr *uint16, err error) { } func envToDurationPtr(envKey string) (durationPtr *time.Duration, err error) { - s := getCleanedEnv(envKey) + s := env.Get(envKey) if s == "" { return nil, nil //nolint:nilnil } diff --git a/internal/configuration/sources/env/log.go b/internal/configuration/sources/env/log.go index c5609fb2..9b499bc7 100644 --- a/internal/configuration/sources/env/log.go +++ b/internal/configuration/sources/env/log.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/log" ) @@ -19,7 +20,7 @@ func readLog() (log settings.Log, err error) { } func readLogLevel() (level *log.Level, err error) { - s := getCleanedEnv("LOG_LEVEL") + s := env.Get("LOG_LEVEL") if s == "" { return nil, nil //nolint:nilnil } diff --git a/internal/configuration/sources/env/openvpn.go b/internal/configuration/sources/env/openvpn.go index 2653cfda..99d4262d 100644 --- a/internal/configuration/sources/env/openvpn.go +++ b/internal/configuration/sources/env/openvpn.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/govalid/binary" ) @@ -15,10 +16,10 @@ func (s *Source) readOpenVPN() ( "OPENVPN_KEY_PASSPHRASE", "OPENVPN_ENCRYPTED_KEY"}, err) }() - openVPN.Version = getCleanedEnv("OPENVPN_VERSION") + openVPN.Version = env.Get("OPENVPN_VERSION") openVPN.User = s.readOpenVPNUser() openVPN.Password = s.readOpenVPNPassword() - confFile := getCleanedEnv("OPENVPN_CUSTOM_CONFIG") + confFile := env.Get("OPENVPN_CUSTOM_CONFIG") if confFile != "" { openVPN.ConfFile = &confFile } @@ -26,7 +27,7 @@ func (s *Source) readOpenVPN() ( ciphersKey, _ := s.getEnvWithRetro("OPENVPN_CIPHERS", "OPENVPN_CIPHER") openVPN.Ciphers = envToCSV(ciphersKey) - auth := getCleanedEnv("OPENVPN_AUTH") + auth := env.Get("OPENVPN_AUTH") if auth != "" { openVPN.Auth = &auth } @@ -56,7 +57,7 @@ func (s *Source) readOpenVPN() ( return openVPN, fmt.Errorf("environment variable OPENVPN_VERBOSITY: %w", err) } - flagsStr := getCleanedEnv("OPENVPN_FLAGS") + flagsStr := env.Get("OPENVPN_FLAGS") if flagsStr != "" { openVPN.Flags = strings.Fields(flagsStr) } @@ -88,7 +89,7 @@ func (s *Source) readOpenVPNPassword() (password *string) { func (s *Source) readOpenVPNKeyPassphrase() (passphrase *string) { passphrase = new(string) - *passphrase = getCleanedEnv("OPENVPN_KEY_PASSPHRASE") + *passphrase = env.Get("OPENVPN_KEY_PASSPHRASE") if *passphrase == "" { return nil } diff --git a/internal/configuration/sources/env/openvpnselection.go b/internal/configuration/sources/env/openvpnselection.go index aef7a18a..e606ec6a 100644 --- a/internal/configuration/sources/env/openvpnselection.go +++ b/internal/configuration/sources/env/openvpnselection.go @@ -7,12 +7,13 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/constants" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/govalid/port" ) func (s *Source) readOpenVPNSelection() ( selection settings.OpenVPNSelection, err error) { - confFile := getCleanedEnv("OPENVPN_CUSTOM_CONFIG") + confFile := env.Get("OPENVPN_CUSTOM_CONFIG") if confFile != "" { selection.ConfFile = &confFile } diff --git a/internal/configuration/sources/env/pprof.go b/internal/configuration/sources/env/pprof.go index eef0f93f..cf31d4de 100644 --- a/internal/configuration/sources/env/pprof.go +++ b/internal/configuration/sources/env/pprof.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/qdm12/gluetun/internal/pprof" + "github.com/qdm12/gosettings/sources/env" ) func readPprof() (settings pprof.Settings, err error) { @@ -22,7 +23,7 @@ func readPprof() (settings pprof.Settings, err error) { return settings, fmt.Errorf("environment variable PPROF_MUTEX_PROFILE_RATE: %w", err) } - settings.HTTPServer.Address = getCleanedEnv("PPROF_HTTP_SERVER_ADDRESS") + settings.HTTPServer.Address = env.Get("PPROF_HTTP_SERVER_ADDRESS") return settings, nil } diff --git a/internal/configuration/sources/env/provider.go b/internal/configuration/sources/env/provider.go index 98124f94..7ab44b16 100644 --- a/internal/configuration/sources/env/provider.go +++ b/internal/configuration/sources/env/provider.go @@ -7,6 +7,7 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/constants/vpn" + "github.com/qdm12/gosettings/sources/env" ) func (s *Source) readProvider(vpnType string) (provider settings.Provider, err error) { @@ -32,7 +33,7 @@ func (s *Source) readProvider(vpnType string) (provider settings.Provider, err e func (s *Source) readVPNServiceProvider(vpnType string) (vpnProviderPtr *string) { _, value := s.getEnvWithRetro("VPN_SERVICE_PROVIDER", "VPNSP") if value == "" { - if vpnType != vpn.Wireguard && getCleanedEnv("OPENVPN_CUSTOM_CONFIG") != "" { + if vpnType != vpn.Wireguard && env.Get("OPENVPN_CUSTOM_CONFIG") != "" { // retro compatibility return stringPtr(providers.Custom) } diff --git a/internal/configuration/sources/env/publicip.go b/internal/configuration/sources/env/publicip.go index a57b10d3..a9f93d48 100644 --- a/internal/configuration/sources/env/publicip.go +++ b/internal/configuration/sources/env/publicip.go @@ -5,6 +5,7 @@ import ( "time" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) func (s *Source) readPublicIP() (publicIP settings.PublicIP, err error) { @@ -19,7 +20,7 @@ func (s *Source) readPublicIP() (publicIP settings.PublicIP, err error) { } func readPublicIPPeriod() (period *time.Duration, err error) { - s := getCleanedEnv("PUBLICIP_PERIOD") + s := env.Get("PUBLICIP_PERIOD") if s == "" { return nil, nil //nolint:nilnil } diff --git a/internal/configuration/sources/env/reader.go b/internal/configuration/sources/env/reader.go index 3fc006fa..0fde429e 100644 --- a/internal/configuration/sources/env/reader.go +++ b/internal/configuration/sources/env/reader.go @@ -2,6 +2,7 @@ package env import ( "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) type Source struct { @@ -106,12 +107,12 @@ func (s *Source) getEnvWithRetro(currentKey string, // We check retro-compatibility keys first since // the current key might be set in the Dockerfile. for _, key = range retroKeys { - value = getCleanedEnv(key) + value = env.Get(key) if value != "" { s.onRetroActive(key, currentKey) return key, value } } - return currentKey, getCleanedEnv(currentKey) + return currentKey, env.Get(currentKey) } diff --git a/internal/configuration/sources/env/server.go b/internal/configuration/sources/env/server.go index 680ec726..d3ef2188 100644 --- a/internal/configuration/sources/env/server.go +++ b/internal/configuration/sources/env/server.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/govalid/binary" ) @@ -19,7 +20,7 @@ func (s *Source) readControlServer() (controlServer settings.ControlServer, err } func readControlServerLog() (enabled *bool, err error) { - s := getCleanedEnv("HTTP_CONTROL_SERVER_LOG") + s := env.Get("HTTP_CONTROL_SERVER_LOG") log, err := binary.Validate(s) if err != nil { return nil, fmt.Errorf("environment variable HTTP_CONTROL_SERVER_LOG: %w", err) diff --git a/internal/configuration/sources/env/serverselection.go b/internal/configuration/sources/env/serverselection.go index 148b4b49..680c4cb8 100644 --- a/internal/configuration/sources/env/serverselection.go +++ b/internal/configuration/sources/env/serverselection.go @@ -9,6 +9,7 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/constants/providers" + "github.com/qdm12/gosettings/sources/env" ) var ( @@ -48,7 +49,7 @@ func (s *Source) readServerSelection(vpnProvider, vpnType string) ( serverNamesKey, _ := s.getEnvWithRetro("SERVER_NAMES", "SERVER_NAME") ss.Names = envToCSV(serverNamesKey) - if csv := getCleanedEnv("SERVER_NUMBER"); csv != "" { + if csv := env.Get("SERVER_NUMBER"); csv != "" { numbersStrings := strings.Split(csv, ",") numbers := make([]uint16, len(numbersStrings)) for i, numberString := range numbersStrings { diff --git a/internal/configuration/sources/env/system.go b/internal/configuration/sources/env/system.go index 78233d6a..b4adf3d5 100644 --- a/internal/configuration/sources/env/system.go +++ b/internal/configuration/sources/env/system.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) var ( @@ -25,7 +26,7 @@ func (s *Source) readSystem() (system settings.System, err error) { return system, err } - system.Timezone = getCleanedEnv("TZ") + system.Timezone = env.Get("TZ") return system, nil } diff --git a/internal/configuration/sources/env/updater.go b/internal/configuration/sources/env/updater.go index 320f13fb..71c718f4 100644 --- a/internal/configuration/sources/env/updater.go +++ b/internal/configuration/sources/env/updater.go @@ -5,6 +5,7 @@ import ( "time" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) func readUpdater() (updater settings.Updater, err error) { @@ -29,7 +30,7 @@ func readUpdater() (updater settings.Updater, err error) { } func readUpdaterPeriod() (period *time.Duration, err error) { - s := getCleanedEnv("UPDATER_PERIOD") + s := env.Get("UPDATER_PERIOD") if s == "" { return nil, nil //nolint:nilnil } diff --git a/internal/configuration/sources/env/version.go b/internal/configuration/sources/env/version.go index 590d2946..2574fe53 100644 --- a/internal/configuration/sources/env/version.go +++ b/internal/configuration/sources/env/version.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/govalid/binary" ) @@ -17,7 +18,7 @@ func readVersion() (version settings.Version, err error) { } func readVersionEnabled() (enabled *bool, err error) { - s := getCleanedEnv("VERSION_INFORMATION") + s := env.Get("VERSION_INFORMATION") if s == "" { return nil, nil //nolint:nilnil } diff --git a/internal/configuration/sources/env/vpn.go b/internal/configuration/sources/env/vpn.go index c4baafdf..7900757e 100644 --- a/internal/configuration/sources/env/vpn.go +++ b/internal/configuration/sources/env/vpn.go @@ -5,10 +5,11 @@ import ( "strings" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) func (s *Source) readVPN() (vpn settings.VPN, err error) { - vpn.Type = strings.ToLower(getCleanedEnv("VPN_TYPE")) + vpn.Type = strings.ToLower(env.Get("VPN_TYPE")) vpn.Provider, err = s.readProvider(vpn.Type) if err != nil { diff --git a/internal/configuration/sources/env/wireguard.go b/internal/configuration/sources/env/wireguard.go index acc561b9..6bfe821c 100644 --- a/internal/configuration/sources/env/wireguard.go +++ b/internal/configuration/sources/env/wireguard.go @@ -3,10 +3,10 @@ package env import ( "fmt" "net/netip" - "os" "strings" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" ) func (s *Source) readWireguard() (wireguard settings.Wireguard, err error) { @@ -16,7 +16,7 @@ func (s *Source) readWireguard() (wireguard settings.Wireguard, err error) { wireguard.PrivateKey = envToStringPtr("WIREGUARD_PRIVATE_KEY") wireguard.PreSharedKey = envToStringPtr("WIREGUARD_PRESHARED_KEY") _, wireguard.Interface = s.getEnvWithRetro("VPN_INTERFACE", "WIREGUARD_INTERFACE") - wireguard.Implementation = os.Getenv("WIREGUARD_IMPLEMENTATION") + wireguard.Implementation = env.Get("WIREGUARD_IMPLEMENTATION") wireguard.Addresses, err = s.readWireguardAddresses() if err != nil { return wireguard, err // already wrapped diff --git a/internal/configuration/sources/env/wireguardselection.go b/internal/configuration/sources/env/wireguardselection.go index b9078f81..952cc854 100644 --- a/internal/configuration/sources/env/wireguardselection.go +++ b/internal/configuration/sources/env/wireguardselection.go @@ -5,6 +5,7 @@ import ( "net/netip" "github.com/qdm12/gluetun/internal/configuration/settings" + "github.com/qdm12/gosettings/sources/env" "github.com/qdm12/govalid/port" ) @@ -20,7 +21,7 @@ func (s *Source) readWireguardSelection() ( return selection, err } - selection.PublicKey = getCleanedEnv("WIREGUARD_PUBLIC_KEY") + selection.PublicKey = env.Get("WIREGUARD_PUBLIC_KEY") return selection, nil } diff --git a/internal/configuration/sources/secrets/helpers.go b/internal/configuration/sources/secrets/helpers.go index fba038d6..e5e23f9d 100644 --- a/internal/configuration/sources/secrets/helpers.go +++ b/internal/configuration/sources/secrets/helpers.go @@ -2,26 +2,15 @@ package secrets import ( "fmt" - "os" - "strings" "github.com/qdm12/gluetun/internal/configuration/sources/files" "github.com/qdm12/gluetun/internal/openvpn/extract" + "github.com/qdm12/gosettings/sources/env" ) -// getCleanedEnv returns an environment variable value with -// surrounding spaces and trailing new line characters removed. -func getCleanedEnv(envKey string) (value string) { - value = os.Getenv(envKey) - value = strings.TrimSpace(value) - value = strings.TrimSuffix(value, "\r\n") - value = strings.TrimSuffix(value, "\n") - return value -} - func readSecretFileAsStringPtr(secretPathEnvKey, defaultSecretPath string) ( stringPtr *string, err error) { - path := getCleanedEnv(secretPathEnvKey) + path := env.Get(secretPathEnvKey) if path == "" { path = defaultSecretPath }