From 99e43fe3401e62cc462ce55506a9675cc8f8575f Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 4 Nov 2024 03:29:26 +0800 Subject: [PATCH] (non-breaking) rebrand changed environment variables --- .trunk/trunk.yaml | 5 ++- internal/common/env.go | 72 ++++++++++++++++--------------- internal/route/provider/docker.go | 2 +- internal/setup.go | 2 +- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 1c2d6d9..d62d64a 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -17,6 +17,9 @@ runtimes: - go@1.23.2 # This is the section where you manage your linters. (https://docs.trunk.io/check/configuration) lint: + disabled: + - markdownlint + - yamllint enabled: - hadolint@2.12.0 - actionlint@1.7.3 @@ -24,14 +27,12 @@ lint: - git-diff-check - gofmt@1.20.4 - golangci-lint@1.61.0 - - markdownlint@0.42.0 - osv-scanner@1.9.0 - oxipng@9.1.2 - prettier@3.3.3 - shellcheck@0.10.0 - shfmt@3.6.0 - trufflehog@3.82.7 - - yamllint@1.35.1 actions: disabled: - trunk-announce diff --git a/internal/common/env.go b/internal/common/env.go index ba09763..9b615f6 100644 --- a/internal/common/env.go +++ b/internal/common/env.go @@ -12,31 +12,33 @@ import ( ) var ( - NoSchemaValidation = GetEnvBool("GOPROXY_NO_SCHEMA_VALIDATION", true) - IsTest = GetEnvBool("GOPROXY_TEST", false) || strings.HasSuffix(os.Args[0], ".test") - IsDebug = GetEnvBool("GOPROXY_DEBUG", IsTest) - IsDebugSkipAuth = GetEnvBool("GOPROXY_DEBUG_SKIP_AUTH", false) - IsTrace = GetEnvBool("GOPROXY_TRACE", false) && IsDebug + prefixes = []string{"GODOXY_", "GOPROXY_", ""} + + NoSchemaValidation = GetEnvBool("NO_SCHEMA_VALIDATION", true) + IsTest = GetEnvBool("TEST", false) || strings.HasSuffix(os.Args[0], ".test") + IsDebug = GetEnvBool("DEBUG", IsTest) + IsDebugSkipAuth = GetEnvBool("DEBUG_SKIP_AUTH", false) + IsTrace = GetEnvBool("TRACE", false) && IsDebug ProxyHTTPAddr, ProxyHTTPHost, ProxyHTTPPort, - ProxyHTTPURL = GetAddrEnv("GOPROXY_HTTP_ADDR", ":80", "http") + ProxyHTTPURL = GetAddrEnv("HTTP_ADDR", ":80", "http") ProxyHTTPSAddr, ProxyHTTPSHost, ProxyHTTPSPort, - ProxyHTTPSURL = GetAddrEnv("GOPROXY_HTTPS_ADDR", ":443", "https") + ProxyHTTPSURL = GetAddrEnv("HTTPS_ADDR", ":443", "https") APIHTTPAddr, APIHTTPHost, APIHTTPPort, - APIHTTPURL = GetAddrEnv("GOPROXY_API_ADDR", "127.0.0.1:8888", "http") + APIHTTPURL = GetAddrEnv("API_ADDR", "127.0.0.1:8888", "http") - APIJWTSecret = decodeJWTKey(GetEnv("GOPROXY_API_JWT_SECRET", "")) - APIJWTTokenTTL = GetDurationEnv("GOPROXY_API_JWT_TOKEN_TTL", time.Hour) - APIUser = GetEnv("GOPROXY_API_USER", "admin") - APIPasswordHash = HashPassword(GetEnv("GOPROXY_API_PASSWORD", "password")) + APIJWTSecret = decodeJWTKey(GetEnvString("API_JWT_SECRET", "")) + APIJWTTokenTTL = GetDurationEnv("API_JWT_TOKEN_TTL", time.Hour) + APIUser = GetEnvString("API_USER", "admin") + APIPasswordHash = HashPassword(GetEnvString("API_PASSWORD", "password")) ) func init() { @@ -45,28 +47,38 @@ func init() { } } -func GetEnvBool(key string, defaultValue bool) bool { - value, ok := os.LookupEnv(key) +func GetEnv[T any](key string, defaultValue T, parser func(string) (T, error)) T { + var value string + var ok bool + for _, prefix := range prefixes { + value, ok = os.LookupEnv(prefix + key) + if ok && value != "" { + break + } + } if !ok || value == "" { return defaultValue } - b, err := strconv.ParseBool(value) - if err != nil { - log.Fatal().Msgf("env %s: invalid boolean value: %s", key, value) + parsed, err := parser(value) + if err == nil { + return parsed } - return b + log.Fatal().Err(err).Msgf("env %s: invalid %T value: %s", key, parsed, value) + return defaultValue } -func GetEnv(key, defaultValue string) string { - value, ok := os.LookupEnv(key) - if !ok || value == "" { - value = defaultValue - } - return value +func GetEnvString(key string, defaultValue string) string { + return GetEnv(key, defaultValue, func(s string) (string, error) { + return s, nil + }) +} + +func GetEnvBool(key string, defaultValue bool) bool { + return GetEnv(key, defaultValue, strconv.ParseBool) } func GetAddrEnv(key, defaultValue, scheme string) (addr, host, port, fullURL string) { - addr = GetEnv(key, defaultValue) + addr = GetEnvString(key, defaultValue) host, port, err := net.SplitHostPort(addr) if err != nil { log.Fatal().Msgf("env %s: invalid address: %s", key, addr) @@ -79,13 +91,5 @@ func GetAddrEnv(key, defaultValue, scheme string) (addr, host, port, fullURL str } func GetDurationEnv(key string, defaultValue time.Duration) time.Duration { - value, ok := os.LookupEnv(key) - if !ok || value == "" { - return defaultValue - } - d, err := time.ParseDuration(value) - if err != nil { - log.Fatal().Msgf("env %s: invalid duration value: %s", key, value) - } - return d + return GetEnv(key, defaultValue, time.ParseDuration) } diff --git a/internal/route/provider/docker.go b/internal/route/provider/docker.go index c1dca89..0cb5ea8 100755 --- a/internal/route/provider/docker.go +++ b/internal/route/provider/docker.go @@ -31,7 +31,7 @@ var ( func DockerProviderImpl(name, dockerHost string, explicitOnly bool) (ProviderImpl, error) { if dockerHost == common.DockerHostFromEnv { - dockerHost = common.GetEnv("DOCKER_HOST", client.DefaultDockerHost) + dockerHost = common.GetEnvString("DOCKER_HOST", client.DefaultDockerHost) } return &DockerProvider{ name, diff --git a/internal/setup.go b/internal/setup.go index 708323b..d8611db 100644 --- a/internal/setup.go +++ b/internal/setup.go @@ -12,7 +12,7 @@ import ( ) var ( - branch = common.GetEnv("GOPROXY_BRANCH", "v0.7") + branch = common.GetEnvString("BRANCH", "v0.7") baseURL = "https://github.com/yusing/go-proxy/raw/" + branch requiredConfigs = []Config{ {common.ConfigBasePath, true, false, ""},