Skip to content

Conversation

@utkarshkgithub
Copy link

The issue was CLI was ignoring configuration values from ~/.kagent/config.yaml and always using hardcoded defaults.

Solution

  • Move config.Init() and config.Get() early in the main function, before setting up command flags
  • Use loaded config values as flag defaults instead of hardcoded values
  • Add fallback to hardcoded defaults when config file is missing or values are empty
  • This ensures all CLI commands respect the user's configuration file

Changes

  • Modified go/cli/cmd/kagent/main.go to load config early and use config values as flag defaults

Closes #973

Copilot AI review requested due to automatic review settings October 1, 2025 15:41
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

This PR fixes an issue where the CLI was ignoring configuration values from ~/.kagent/config.yaml and always using hardcoded defaults. The fix ensures that user configuration files are properly respected by loading the config early and using those values as flag defaults.

  • Moves config initialization to occur before setting up command flags
  • Implements fallback logic to use hardcoded defaults when config values are empty
  • Uses loaded config values as the default values for CLI flags

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 59 to 77
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}

namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}

outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}

timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

[nitpick] This fallback logic is repetitive and could be simplified using a helper function or ternary-like pattern. Consider creating a helper function like getConfigWithDefault(configValue, defaultValue string) string to reduce code duplication.

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 77
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}

namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}

outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}

timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

Variable names should follow Go naming conventions. Use camelCase consistently: kagentURL, namespace, outputFormat, and timeout should be kagentURL, namespace, outputFormat, and timeout respectively. However, the indentation is inconsistent - these lines use spaces instead of tabs.

Suggested change
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 77
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}

namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}

outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}

timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

Variable names should follow Go naming conventions. Use camelCase consistently: kagentURL, namespace, outputFormat, and timeout should be kagentURL, namespace, outputFormat, and timeout respectively. However, the indentation is inconsistent - these lines use spaces instead of tabs.

Suggested change
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 77
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}

namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}

outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}

timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

Variable names should follow Go naming conventions. Use camelCase consistently: kagentURL, namespace, outputFormat, and timeout should be kagentURL, namespace, outputFormat, and timeout respectively. However, the indentation is inconsistent - these lines use spaces instead of tabs.

Suggested change
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 77
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}

namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}

outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}

timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
Copy link

Copilot AI Oct 1, 2025

Choose a reason for hiding this comment

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

Variable names should follow Go naming conventions. Use camelCase consistently: kagentURL, namespace, outputFormat, and timeout should be kagentURL, namespace, outputFormat, and timeout respectively. However, the indentation is inconsistent - these lines use spaces instead of tabs.

Suggested change
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}
kagentURL := cfg.KAgentURL
if kagentURL == "" {
kagentURL = "http://localhost:8083"
}
namespace := cfg.Namespace
if namespace == "" {
namespace = "kagent"
}
outputFormat := cfg.OutputFormat
if outputFormat == "" {
outputFormat = "table"
}
timeout := cfg.Timeout
if timeout == 0 {
timeout = 300 * time.Second
}

Copilot uses AI. Check for mistakes.
}

// Set fallback defaults if config values are empty
kagentURL := cfg.KAgentURL
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the defaults are set in the Init func in the config.go (not sure if using viper is the right thing to do there, but we should set the defaults there if the config is not available)

Copy link
Author

Choose a reason for hiding this comment

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

You're absolutely right, I can see the defaults are already in Init(). I'll remove the redundant fallbacks in main.go.

Copy link
Author

Choose a reason for hiding this comment

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

i did the changes that uses the Init() function using viper.SetDefault() now

@utkarshkgithub utkarshkgithub force-pushed the utkarshkgithub/fixcliconfignamespace973 branch from 725cfc6 to 06ed213 Compare October 1, 2025 16:09
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")
Copy link
Collaborator

Choose a reason for hiding this comment

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

you removed the 300*time.Second, yet you left the time import -- this tells me you didn't even run the code, because if you would, you'd see that it fails with:

❯  go run cli/cmd/kagent/main.go dashboard
# command-line-arguments
cli/cmd/kagent/main.go:9:2: "time" imported and not used

Please run and test your code; and make sure it actually works and fixes the issue.

Copy link
Collaborator

@peterj peterj left a comment

Choose a reason for hiding this comment

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

I don't think you compiled & tested this code at all:

  go run cli/cmd/kagent/main.go dashboard
# command-line-arguments
cli/cmd/kagent/main.go:9:2: "time" imported and not used

Load config early and use values from ~/.kagent/config.yaml as flag
defaults instead of hardcoded values. Falls back to defaults when
config is missing.

Resolves kagent-dev#973

Signed-off-by: Utkarsh Kumar <[email protected]>
@utkarshkgithub utkarshkgithub force-pushed the utkarshkgithub/fixcliconfignamespace973 branch from 06ed213 to f145529 Compare October 1, 2025 18:19
@utkarshkgithub
Copy link
Author

I am sorry for that .I removed the unused time import and have now tested the fix.

@peterj
Copy link
Collaborator

peterj commented Oct 3, 2025

So this still doesn't work. The reason is that the deployCmd (at the end of the main function) overwrites the namespace in the config with "":

deployCmd.Flags().StringVar(&deployCfg.Config.Namespace, "namespace", "", "Kubernetes namespace to deploy to")

To fix this, we should use cfg.Namespace instead of "" in the deployCmd.

Copy link
Contributor

@EItanya EItanya left a comment

Choose a reason for hiding this comment

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

At a more general level I think we should get rid of the config file altogether, I don't think it has really provided any value, and the plumbing isn't correct to make it worth it. Rather let's just rely on flags and defaults as most CLIs do. What do others think?

@utkarshkgithub
Copy link
Author

At a more general level I think we should get rid of the config file altogether, I don't think it has really provided any value, and the plumbing isn't correct to make it worth it. Rather let's just rely on flags and defaults as most CLIs do. What do others think?

yeah i also think config is not that helpful so should i make changes to this issue or add another issue to remove config file altogether?

@EItanya
Copy link
Contributor

EItanya commented Oct 6, 2025

At a more general level I think we should get rid of the config file altogether, I don't think it has really provided any value, and the plumbing isn't correct to make it worth it. Rather let's just rely on flags and defaults as most CLIs do. What do others think?

yeah i also think config is not that helpful so should i make changes to this issue or add another issue to remove config file altogether?

Feel free to make changes to the existing

@FalcoSuessgott
Copy link
Contributor

FalcoSuessgott commented Oct 7, 2025

At a more general level I think we should get rid of the config file altogether, I don't think it has really provided any value, and the plumbing isn't correct to make it worth it. Rather let's just rely on flags and defaults as most CLIs do. What do others think?

FWIW: Im working on a PR that completely refactors the config file parsing and handling, so perhaps give me some days to finish it and then you guys can decide wether you wanna have it or get rid of the config file entirely

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

4 participants