Skip to content

fix: CLI config values are not used#1514

Open
majiayu000 wants to merge 1 commit intokagent-dev:mainfrom
majiayu000:fix/issue-973-cli-config-values-not-used
Open

fix: CLI config values are not used#1514
majiayu000 wants to merge 1 commit intokagent-dev:mainfrom
majiayu000:fix/issue-973-cli-config-values-not-used

Conversation

@majiayu000
Copy link

Fixes #973

Changes

config.Init() was called after Cobra flag registration in main.go, so
~/.kagent/config.yaml values were never used as flag defaults. The hardcoded
defaults in StringVar/DurationVar calls always took precedence.

This moves config.Init() and config.Get() before flag registration so that
config file values serve as defaults when flags are not explicitly provided on
the command line. Also fixes the deploy command's --namespace flag which
shadowed the root persistent flag with its own hardcoded default.

Precedence is now: CLI flag > config file value > built-in default.

Test Plan

  • Existing unit tests pass: go test -race -skip 'TestE2E.*' ./core/cli/...
  • Manual: create ~/.kagent/config.yaml with custom namespace/url, run
    kagent dashboard or kagent get agent and confirm config values are used

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>
@majiayu000 majiayu000 marked this pull request as ready for review March 17, 2026 09:47
@majiayu000 majiayu000 requested a review from EItanya as a code owner March 17, 2026 09:47
Copilot AI review requested due to automatic review settings March 17, 2026 09:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 cfg values (from Viper) instead of hardcoded defaults.
  • Align deploy --namespace default with the root/configured namespace to avoid overriding configured defaults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +36 to +45
// 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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI config values are not used

2 participants