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
+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())
}
+14 -11
View File
@@ -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: