fix: CLI config values are not used#1514
Conversation
config.Init() was called after flag registration, so ~/.kagent/config.yaml values were never used as flag defaults. Move config.Init() and config.Get() before flag registration so config file values serve as defaults when flags are not explicitly provided. Also fix deploy command's --namespace flag to use the loaded config value instead of a hardcoded default. Fixes kagent-dev#973 Signed-off-by: majiayu000 <1835304752@qq.com>
There was a problem hiding this comment.
Pull request overview
Fixes CLI config precedence so values from ~/.kagent/config.yaml are actually used as defaults when users don’t pass explicit flags, addressing #973.
Changes:
- Move
config.Init()+config.Get()ahead of Cobra flag registration so config-file values become flag defaults. - Update root persistent flags to use
cfgvalues (from Viper) instead of hardcoded defaults. - Align
deploy --namespacedefault with the root/configured namespace to avoid overriding configured defaults.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Initialize config before flag registration so config file values are used as defaults | ||
| if err := config.Init(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error initializing config: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| cfg, err := config.Get() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error getting config: %v\n", err) | ||
| os.Exit(1) |
There was a problem hiding this comment.
Good catch on the --config flag behavior. However, the pflag.StringVar call in config.Init() (in config.go:43) is pre-existing code that was not introduced or modified by this PR — config.go has zero changes in this diff.
This PR only moves the config.Init() call earlier in main.go and replaces hardcoded flag defaults with values loaded from the config file, which is the minimal fix for #973.
The pre-existing issue with --config being registered on the global pflag.CommandLine (rather than on rootCmd.PersistentFlags()) and being unparsed before ReadInConfig() is a valid concern but is out of scope for this bug fix. Happy to open a follow-up issue to address that separately if desired.
There was a problem hiding this comment.
Good catch on the --config flag behavior. However, the pflag.StringVar call in config.Init() (in config.go:43) is pre-existing code that was not introduced or modified by this PR — config.go has zero changes in this diff.
This PR only moves the config.Init() call earlier in main.go and replaces hardcoded flag defaults with values loaded from the config file, which is the minimal fix for #973.
The pre-existing issue with --config being registered on the global pflag.CommandLine (rather than on rootCmd.PersistentFlags()) and being unparsed before ReadInConfig() is a valid concern but is out of scope for this bug fix. Happy to open a follow-up issue to address that separately if desired.
Fixes #973
Changes
config.Init()was called after Cobra flag registration inmain.go, so~/.kagent/config.yamlvalues were never used as flag defaults. The hardcodeddefaults in
StringVar/DurationVarcalls always took precedence.This moves
config.Init()andconfig.Get()before flag registration so thatconfig file values serve as defaults when flags are not explicitly provided on
the command line. Also fixes the deploy command's
--namespaceflag whichshadowed the root persistent flag with its own hardcoded default.
Precedence is now: CLI flag > config file value > built-in default.
Test Plan
go test -race -skip 'TestE2E.*' ./core/cli/...~/.kagent/config.yamlwith custom namespace/url, runkagent dashboardorkagent get agentand confirm config values are used