Fix apikey subcommand flag parsing (flags must follow the subcommand)
This commit is contained in:
@@ -48,21 +48,23 @@ func main() {
|
||||
|
||||
// runAPIKey:密钥管理子命令(create/list/revoke)。
|
||||
func runAPIKey(args []string) int {
|
||||
fs := flag.NewFlagSet("apikey", flag.ExitOnError)
|
||||
// 子命令在前、flag 在后:先剥出子命令再解析(Go flag 遇位置参数即停)。
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "用法:agent-system apikey <create|list|revoke> [--owner x --name y]")
|
||||
return 2
|
||||
}
|
||||
sub := args[0]
|
||||
fs := flag.NewFlagSet("apikey "+sub, flag.ExitOnError)
|
||||
data := fs.String("data", "./data", "数据目录")
|
||||
dsn := fs.String("dsn", "", "PostgreSQL 档 DSN(空 = 嵌入式档)")
|
||||
owner := fs.String("owner", "", "租户名(owner)")
|
||||
name := fs.String("name", "default", "密钥名称")
|
||||
fs.Parse(args)
|
||||
if len(fs.Args()) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "用法:agent-system apikey <create|list|revoke> [--owner x --name y]")
|
||||
return 2
|
||||
}
|
||||
fs.Parse(args[1:])
|
||||
if *owner == "" {
|
||||
fmt.Fprintln(os.Stderr, "apikey: --owner 必填")
|
||||
return 2
|
||||
}
|
||||
switch fs.Args()[0] {
|
||||
switch sub {
|
||||
case "create":
|
||||
key, err := app.CreateAPIKey(*dsn, *data, *owner, *name)
|
||||
if err != nil {
|
||||
@@ -94,7 +96,7 @@ func runAPIKey(args []string) int {
|
||||
fmt.Println("revoked")
|
||||
return 0
|
||||
default:
|
||||
fmt.Fprintln(os.Stderr, "apikey: 未知子命令", fs.Args()[0])
|
||||
fmt.Fprintln(os.Stderr, "apikey: 未知子命令", sub)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user