From 939338477c082f5ed73ded00df042f8fa02e10e515932b5e9ec705271d82a149 Mon Sep 17 00:00:00 2001 From: Daan Selen Date: Thu, 23 Apr 2026 22:42:35 +0200 Subject: [PATCH] feat: replace stdlig flag with pflag by spf13 --- cmd/server/main.go | 4 ++-- go.mod | 1 + go.sum | 2 ++ internal/server/api/api.go | 2 +- internal/server/bootstrap/bootstrap.go | 25 ++++++++++++++----------- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index f684e11..6f25924 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -32,11 +32,11 @@ func main() { } // kick off the watchdog depending on the environment variables - if env.WatchdogEnabled { + if env.Watchdog { slog.Info("kicking off database watchdog", "watchdog_interval", env.WatchdogInterval) database.KickoffDatabaseWatchdog(env, db) } else { - slog.Info("skipping database watchdog", "watchdog_enabled", env.WatchdogEnabled) + slog.Info("skipping database watchdog", "watchdog_enabled", env.Watchdog) } // TO DO make gin log as json diff --git a/go.mod b/go.mod index 1be4ee4..a904a3e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.26.1 require ( github.com/gin-gonic/gin v1.12.0 github.com/google/uuid v1.6.0 + github.com/spf13/pflag v1.0.10 gorm.io/driver/sqlite v1.6.0 gorm.io/gorm v1.31.1 ) diff --git a/go.sum b/go.sum index 4cdd4db..cb7a3d6 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,8 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/internal/server/api/api.go b/internal/server/api/api.go index c925a15..c646251 100644 --- a/internal/server/api/api.go +++ b/internal/server/api/api.go @@ -24,7 +24,7 @@ func KickoffApi(logger *slog.Logger, env bootstrap.Environment, db *gorm.DB) { r.Use(middleware.SlogMiddleware(logger)) r.Use(gin.Recovery()) - if env.AuthenticationEnabled { + if env.Authentication { slog.Debug("activating authentication middleware") // only log when actually doign the thing it logs to do r.Use(middleware.AuthMiddleware()) } diff --git a/internal/server/bootstrap/bootstrap.go b/internal/server/bootstrap/bootstrap.go index 0eec7fd..810b2ec 100644 --- a/internal/server/bootstrap/bootstrap.go +++ b/internal/server/bootstrap/bootstrap.go @@ -1,10 +1,11 @@ package bootstrap import ( - "flag" "os" "reflect" "strconv" + + flag "github.com/spf13/pflag" ) type Environment struct { @@ -12,13 +13,13 @@ type Environment struct { Codename string `env:"CODENAME" default:"Magical Anomaly" flag:"codename"` LogLevel string `env:"LOG_LEVEL" default:"debug" flag:"log-level"` - DataDirectory string `env:"DATA_DIR" default:"./data" flag:"data-dir" usage:"option to specify where the state data gets stored"` - ContentDirectory string `env:"CONTENT_DIR" default:"./content" flag:"content-dir" usage:"option to specify where the content gets stored"` - Hostname string `env:"HOSTNAME" default:"0.0.0.0" flag:"hostname" usage:"option specify the address/hostname to bind the api server to"` - Port int `env:"PORT" default:"8080" flag:"port" usage:"option to specify the port to bind the api server to"` - AuthenticationEnabled bool `env:"AUTH_ENABLED" default:"true" flag:"auth" usage:"option to disable authentication"` + DataDirectory string `env:"DATA_DIR" default:"./data" flag:"data-dir" usage:"option to specify where the state data gets stored"` + ContentDirectory string `env:"CONTENT_DIR" default:"./content" flag:"content-dir" usage:"option to specify where the content gets stored"` + Hostname string `env:"HOSTNAME" default:"0.0.0.0" flag:"hostname" usage:"option to specify the address/hostname to bind the api server to"` + Port int `env:"PORT" default:"8080" flag:"port" usage:"option to specify the port to bind the api server to"` + Authentication bool `env:"AUTHENTICATION" default:"true" flag:"authentication" usage:"option to disable authentication"` - WatchdogEnabled bool `env:"WATCHDOG_ENABLED" default:"true" flag:"watchdog" usage:"option to disable watchdog"` + Watchdog bool `env:"WATCHDOG" default:"true" flag:"watchdog" usage:"option to disable watchdog"` WatchdogInterval int `env:"WATCHDOG_INTERVAL" default:"60" flag:"watchdog-interval" usage:"option to specify the interval in second(s) on which watchdog runs"` WatchdogSyncMode string `env:"WATCHDOG_SYNC_MODE" default:"strict" flag:"watchdog-mode" usage:"option to specify the mode watchdog will run with: strict|sync|dry"` } @@ -37,7 +38,7 @@ func loadFromEnv(env *Environment) { // If the variable is not found or is empty raw, ok := os.LookupEnv(key) - if !ok || raw == "" { + if !ok || len(raw) == 0 { raw = fallback } @@ -68,17 +69,19 @@ func bindFlags(env *Environment) { flagName := field.Tag.Get("flag") flagUsage := field.Tag.Get("usage") - if flagName == "" { + if len(flagName) == 0 { continue } switch valueField.Kind() { case reflect.String: - flag.StringVar(valueField.Addr().Interface().(*string), flagName, + flag.StringVar( + valueField.Addr().Interface().(*string), flagName, valueField.String(), flagUsage, ) case reflect.Int: - flag.IntVar(valueField.Addr().Interface().(*int), flagName, + flag.IntVar( + valueField.Addr().Interface().(*int), flagName, int(valueField.Int()), flagUsage, ) case reflect.Bool: