Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions go/core/cli/cmd/kagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Comment on lines +36 to +45
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.

}

rootCmd := &cobra.Command{
Use: "kagent",
Expand All @@ -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,
}
Expand Down Expand Up @@ -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)")

Expand Down Expand Up @@ -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)

Expand Down
Loading