Go CLI: fix key commands (#16306)

### What problem does this PR solve?

```
RAGFlow(api/default)> set key 'ragflow-JgnarFSCUiV99oOvvMDei7ZzZg1cVlqGd1AMHrHeKE4';
SUCCESS
RAGFlow(api/default)> unset key;
SUCCESS

RAGFlow(api/default)> list provider 'zhipu-ai' instances;

RAGFlow(api/default)> list providers;

RAGFlow(api/default)> list available providers;

RAGFlow(api/default)> list provider 'zhipu-ai' instance 'test' models;
```

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-24 18:48:09 +08:00
committed by GitHub
parent 9a91564194
commit 1fc02606ea
8 changed files with 202 additions and 196 deletions

View File

@@ -37,7 +37,7 @@ type APIServerConfig struct {
Host string `yaml:"host"`
UserName *string `yaml:"user_name"`
UserPassword *string `yaml:"password"`
ApiToken *string `yaml:"api_token"`
APIKey *string `yaml:"api_key"`
KeyFile *string `yaml:"key_file"`
IP string
Port int
@@ -46,7 +46,7 @@ type APIServerConfig struct {
// ConfigFile represents the rf.yml configuration file structure
type ConfigFile struct {
Host string `yaml:"host"` // default API server host
APIToken string `yaml:"api_token"` // default API server api token
APIKey string `yaml:"api_key"` // default API server api key
UserName string `yaml:"user_name"` // default API server user name
Password string `yaml:"password"` // default API server password
APIServerMap map[string]*APIServerConfig `yaml:"api_servers"`
@@ -152,7 +152,7 @@ func ParseArgs(args []string) (*CommandLineConfig, error) {
defaultApiServerConfig := &APIServerConfig{
UserName: nil,
UserPassword: nil,
ApiToken: nil,
APIKey: nil,
}
configFile := "rf.yml"
@@ -194,7 +194,7 @@ func ParseArgs(args []string) (*CommandLineConfig, error) {
}
case "-t", "--token":
if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
defaultApiServerConfig.ApiToken = &args[i+1]
defaultApiServerConfig.APIKey = &args[i+1]
i++
}
case "-u", "--user":
@@ -263,9 +263,9 @@ func ParseArgs(args []string) (*CommandLineConfig, error) {
defaultApiServerConfig.UserPassword = &config.Password
}
}
if config.APIToken != "" {
if defaultApiServerConfig.ApiToken == nil {
defaultApiServerConfig.ApiToken = &config.APIToken
if config.APIKey != "" {
if defaultApiServerConfig.APIKey == nil {
defaultApiServerConfig.APIKey = &config.APIKey
}
}
} else {
@@ -514,15 +514,15 @@ func NewCLIWithConfig(commandLineConfig *CommandLineConfig) (*CLI, error) {
httpClient := NewHTTPClient()
httpClient.Host = apiServerConfig.IP
httpClient.Port = apiServerConfig.Port
if apiServerConfig.ApiToken != nil {
httpClient.APIToken = apiServerConfig.ApiToken
httpClient.useAPIToken = true
if apiServerConfig.APIKey != nil {
httpClient.APIKey = apiServerConfig.APIKey
httpClient.useAPIKey = true
}
cli.APIServerClientMap = map[string]*HTTPClient{
cli.Config.APIClientConfig.CurrentAPIServer: httpClient,
}
// Auto-login if user and password are provided (from config file)
if apiServerConfig.UserName != nil && apiServerConfig.UserPassword != nil && apiServerConfig.ApiToken == nil {
if apiServerConfig.UserName != nil && apiServerConfig.UserPassword != nil && apiServerConfig.APIKey == nil {
if err := cli.LoginUserInteractive(*apiServerConfig.UserName, *apiServerConfig.UserPassword); err != nil {
line.Close()
return nil, fmt.Errorf("auto-login failed: %w", err)
@@ -567,7 +567,7 @@ func (c *CLI) Run() error {
switch cliConfig.CLIMode {
case APIMode:
apiConfig := c.Config.APIClientConfig.APIServerMap[c.Config.APIClientConfig.CurrentAPIServer]
if apiConfig.UserName != nil && apiConfig.UserPassword == nil && apiConfig.ApiToken == nil {
if apiConfig.UserName != nil && apiConfig.UserPassword == nil && apiConfig.APIKey == nil {
// provider username but no password or api token
maxAttempts := 3
for attempt := 1; attempt <= maxAttempts; attempt++ {