[server] Incorporate flags

This commit is contained in:
Abhishek Shroff
2025-05-15 09:55:22 +05:30
parent f3b3f55706
commit 495ee99084
2 changed files with 22 additions and 10 deletions
+15 -3
View File
@@ -9,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"github.com/shroff/phylum/server/internal/command/fs"
@@ -22,6 +23,7 @@ import (
"github.com/shroff/phylum/server/internal/core/storage"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
@@ -41,8 +43,8 @@ func SetupCommand() {
flags.MarkHidden("debug")
viper.BindPFlag("debug", flags.Lookup("debug"))
flags.Bool("trace-queries", false, "Trace Database Queries")
flags.MarkHidden("trace-queries")
flags.Bool("db_trace-queries", false, "Trace Database Queries")
flags.MarkHidden("db_trace-queries")
viper.SetDefault("db.host", "localhost")
viper.SetDefault("db.port", "5432")
@@ -75,6 +77,16 @@ func SetupCommand() {
}
logrus.Info("Loaded config from ", configFile)
}
if err := k.Load(posflag.ProviderWithFlag(cmd.Flags(), ".", k, func(f *pflag.Flag) (string, interface{}) {
if !f.Changed {
return "", ""
}
return strings.ReplaceAll(f.Name, "_", "."), posflag.FlagVal(cmd.Flags(), f)
}), nil); err != nil {
logrus.Fatalf("Unable to load flags: %v", err)
}
var cfg Config
k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "koanf"})
@@ -85,7 +97,7 @@ func SetupCommand() {
db.Config = viper.Sub("db")
// TODO: Hack. flag bindings don't work in viper for nested keys
db.Config.BindPFlag("trace_queries", flags.Lookup("trace-queries"))
db.Config.BindPFlag("trace_queries", flags.Lookup("db_trace-queries"))
storage.Config = viper.Sub("storage")
storage.Config.BindPFlag("location", flags.Lookup("location"))
}
+7 -7
View File
@@ -1,7 +1,7 @@
package command
var defaultConfig = Config{
Database: DBConfig{
DB: DBConfig{
Host: "localhost",
Port: 5432,
Username: "phylum",
@@ -24,10 +24,10 @@ var defaultConfig = Config{
}
type Config struct {
Debug bool `koanf:"debug"`
Database DBConfig `kaonf:"db"`
Storage StorageConfig `koanf:"storage"`
Server ServerConfig `koanf:"server"`
Debug bool `koanf:"debug"`
DB DBConfig `koanf:"db"`
Storage StorageConfig `koanf:"storage"`
Server ServerConfig `koanf:"server"`
}
type DBConfig struct {
@@ -35,8 +35,8 @@ type DBConfig struct {
Port int `koanf:"port"`
Username string `koanf:"username"`
Password string `koanf:"password"`
SkipMigration bool `koanf:"skip_migration"`
TraceQueries bool `koanf:"trace_queries"`
SkipMigration bool `koanf:"skip-migration"`
TraceQueries bool `koanf:"trace-queries"`
}
type ServerConfig struct {