From 133a29a4f456748ededfe457276cac206dd729fb Mon Sep 17 00:00:00 2001 From: majiayu000 <1835304752@qq.com> Date: Tue, 17 Mar 2026 17:22:41 +0800 Subject: [PATCH] fix: load config file values before CLI flag registration 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 #973 Signed-off-by: majiayu000 <1835304752@qq.com> --- go/core/cli/cmd/kagent/main.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/go/core/cli/cmd/kagent/main.go b/go/core/cli/cmd/kagent/main.go index 8792835b0..ef06366b7 100644 --- a/go/core/cli/cmd/kagent/main.go +++ b/go/core/cli/cmd/kagent/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" cli "github.com/kagent-dev/kagent/go/core/cli/internal/cli/agent" "github.com/kagent-dev/kagent/go/core/cli/internal/cli/envdoc" @@ -34,7 +33,17 @@ func main() { cancel() }() - cfg := &config.Config{} + // 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) + } rootCmd := &cobra.Command{ Use: "kagent", @@ -43,11 +52,11 @@ func main() { Run: runInteractive, } - rootCmd.PersistentFlags().StringVar(&cfg.KAgentURL, "kagent-url", "http://localhost:8083", "KAgent URL") - rootCmd.PersistentFlags().StringVarP(&cfg.Namespace, "namespace", "n", "kagent", "Namespace") - rootCmd.PersistentFlags().StringVarP(&cfg.OutputFormat, "output-format", "o", "table", "Output format") - rootCmd.PersistentFlags().BoolVarP(&cfg.Verbose, "verbose", "v", false, "Verbose output") - rootCmd.PersistentFlags().DurationVar(&cfg.Timeout, "timeout", 300*time.Second, "Timeout") + rootCmd.PersistentFlags().StringVar(&cfg.KAgentURL, "kagent-url", cfg.KAgentURL, "KAgent URL") + rootCmd.PersistentFlags().StringVarP(&cfg.Namespace, "namespace", "n", cfg.Namespace, "Namespace") + rootCmd.PersistentFlags().StringVarP(&cfg.OutputFormat, "output-format", "o", cfg.OutputFormat, "Output format") + rootCmd.PersistentFlags().BoolVarP(&cfg.Verbose, "verbose", "v", cfg.Verbose, "Verbose output") + rootCmd.PersistentFlags().DurationVar(&cfg.Timeout, "timeout", cfg.Timeout, "Timeout") installCfg := &cli.InstallCfg{ Config: cfg, } @@ -348,7 +357,7 @@ Examples: // Add flags for deploy command deployCmd.Flags().StringVarP(&deployCfg.Image, "image", "i", "", "Image to use (defaults to localhost:5001/{agentName}:latest)") deployCmd.Flags().StringVar(&deployCfg.EnvFile, "env-file", "", "Path to .env file containing environment variables (including API keys)") - deployCmd.Flags().StringVar(&deployCfg.Config.Namespace, "namespace", "kagent", "Kubernetes namespace to deploy to") + deployCmd.Flags().StringVar(&deployCfg.Config.Namespace, "namespace", cfg.Namespace, "Kubernetes namespace to deploy to") deployCmd.Flags().BoolVar(&deployCfg.DryRun, "dry-run", false, "Output YAML manifests without applying them to the cluster") deployCmd.Flags().StringVar(&deployCfg.Platform, "platform", "", "Target platform for Docker build (e.g., linux/amd64, linux/arm64)") @@ -429,12 +438,6 @@ Examples: rootCmd.AddCommand(installCmd, uninstallCmd, invokeCmd, bugReportCmd, versionCmd, dashboardCmd, getCmd, initCmd, buildCmd, deployCmd, addMcpCmd, runCmd, mcp.NewMCPCmd(), envdoc.NewEnvCmd()) - // Initialize config - if err := config.Init(); err != nil { - fmt.Fprintf(os.Stderr, "Error initializing config: %v\n", err) - os.Exit(1) - } - if err := rootCmd.ExecuteContext(ctx); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err)