fix(settings): use qdm12/gosettings env.Get

This commit is contained in:
Quentin McGaw
2023-05-29 20:43:06 +00:00
parent b961284845
commit 47593928f9
20 changed files with 51 additions and 58 deletions

2
go.mod
View File

@@ -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

6
go.sum
View File

@@ -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=

View File

@@ -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")

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}