Files
ragflow/cmd/ragflow_cli.go
Jin Hai d02eb6b596 Go: refactor CLI (#15728)
### What problem does this PR solve?

```
RAGFlow(user)> add api server 'ccc' host '127.0.0.1:9980';
SUCCESS
RAGFlow(user)> list api server;
+------------+---------------+-----------------+---------+-------------+---------------+
| api_server | api_server_ip | api_server_port | auth    | user_name   | user_password |
+------------+---------------+-----------------+---------+-------------+---------------+
| ccc        | 127.0.0.1     | 9980            | no auth |             |               |
| default    | 127.0.0.1     | 9384            | login   | aaa@aaa.com | ***           |
+------------+---------------+-----------------+---------+-------------+---------------+
RAGFlow(user)> delete api server 'ccc';
SUCCESS
RAGFlow(user)> list api server;
+------------+---------------+-----------------+---------+
| api_server | api_server_ip | api_server_port | auth    |
+------------+---------------+-----------------+---------+
| default    | 127.0.0.1     | 9384            | no auth |
+------------+---------------+-----------------+---------+

RAGFlow(user)> show admin server;
+--------------+-------+
| field        | value |
+--------------+-------+
| admin_server | N/A   |
+--------------+-------+
RAGFlow(user)> add admin server host '127.0.0.1:9880';
SUCCESS
RAGFlow(user)> show admin server;
+-------------------+-----------+
| field             | value     |
+-------------------+-----------+
| admin_server_ip   | 127.0.0.1 |
| admin_server_port | 9880      |
| auth              | no auth   |
+-------------------+-----------+
RAGFlow(user)> delete admin server;
SUCCESS
RAGFlow(user)> show admin server;
+--------------+-------+
| field        | value |
+--------------+-------+
| admin_server | N/A   |
+--------------+-------+

RAGFlow(user)> show current
+-----------------+-------------+
| field           | value       |
+-----------------+-------------+
| api_server_port | 9384        |
| user_name       | aaa@aaa.com |
| user_password   | ***         |
| mode            | api         |
| verbose         | false       |
| api_server      | default     |
| api_server_ip   | 127.0.0.1   |
| auth            | login       |
| output          | table       |
| interactive     | true        |
+-----------------+-------------+
```
### Type of change

- [x] Refactoring

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
2026-06-09 15:22:50 +08:00

135 lines
3.0 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"fmt"
"os"
"os/signal"
"ragflow/internal/common"
"syscall"
"ragflow/internal/cli"
)
func newMain() {
parseArgs, err := cli.ParseArgs(os.Args[1:])
if err != nil {
return
}
if parseArgs.ShowHelp {
cli.PrintUsage()
return
}
//parseArgs.Print()
logLevel := "warn" // Default to warn (quiet mode)
if parseArgs.Verbose {
logLevel = "info"
}
if err = common.Init(logLevel, ""); err != nil {
fmt.Printf("Warning: Failed to initialize logger: %v\n", err)
}
client, err := cli.NewCLIWithConfig(parseArgs)
if err != nil {
fmt.Printf("Failed to create CLI: %v\n", err)
os.Exit(1)
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
client.Cleanup()
os.Exit(0)
}()
if parseArgs.Command != nil {
if err = client.RunSingleCommand(parseArgs.Command); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
} else {
if err = client.NewRun(); err != nil {
fmt.Printf("CLI error: %v\n", err)
os.Exit(1)
}
}
return
}
func main() {
newMain()
//// Parse command line arguments (skip program name)
//args, err := cli.ParseConnectionArgs(os.Args[1:])
//if err != nil {
// fmt.Printf("Error: %v\n", err)
// os.Exit(1)
//}
//
//// Initialize logger with appropriate level
//logLevel := "warn" // Default to warn (quiet mode)
//if args.Verbose {
// logLevel = "info"
//}
//if err = common.Init(logLevel); err != nil {
// fmt.Printf("Warning: Failed to initialize logger: %v\n", err)
//}
//
//// Show help and exit
//if args.ShowHelp {
// cli.PrintUsage()
// os.Exit(0)
//}
//
//// Create CLI instance with parsed arguments
//cliApp, err := cli.NewCLIWithArgs(args)
//if err != nil {
// fmt.Printf("Failed to create CLI: %v\n", err)
// os.Exit(1)
//}
//
//// Handle interrupt signal
//sigChan := make(chan os.Signal, 1)
//signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
//go func() {
// <-sigChan
// cliApp.Cleanup()
// os.Exit(0)
//}()
//
//// Check if we have a single command to execute
//if args.Command != nil {
// // Single command mode
// if err = cliApp.RunSingleCommand(args.Command); err != nil {
// fmt.Printf("Error: %v\n", err)
// os.Exit(1)
// }
//} else {
// // Interactive mode
// if err = cliApp.Run(); err != nil {
// fmt.Printf("CLI error: %v\n", err)
// os.Exit(1)
// }
//}
}