feat: replace stdlig flag with pflag by spf13

This commit is contained in:
2026-04-23 22:42:35 +02:00
parent 5e2043d54e
commit 939338477c
5 changed files with 20 additions and 14 deletions
+2 -2
View File
@@ -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
+1
View File
@@ -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
)
+2
View File
@@ -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=
+1 -1
View File
@@ -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())
}
+11 -8
View File
@@ -1,10 +1,11 @@
package bootstrap
import (
"flag"
"os"
"reflect"
"strconv"
flag "github.com/spf13/pflag"
)
type Environment struct {
@@ -14,11 +15,11 @@ type Environment struct {
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"`
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"`
AuthenticationEnabled bool `env:"AUTH_ENABLED" default:"true" flag:"auth" usage:"option to disable authentication"`
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: