From 11d58d838f3240067609204c40b2669e5736ad3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 21 Sep 2024 03:12:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20cli=20=E8=B7=AF=E7=94=B1=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 + internal/bootstrap/cli.go | 42 ++++-- internal/route/cli.go | 276 +++++++++++++++++++++++++++++++++++++- 4 files changed, 304 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index cf318b7f..860aa7c9 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/swaggo/http-swagger/v2 v2.0.2 github.com/swaggo/swag v1.16.3 github.com/urfave/cli/v2 v2.27.4 + github.com/urfave/cli/v3 v3.0.0-alpha9 go.uber.org/zap v1.27.0 golang.org/x/crypto v0.27.0 golang.org/x/net v0.29.0 diff --git a/go.sum b/go.sum index 2444f87e..56df23b3 100644 --- a/go.sum +++ b/go.sum @@ -323,6 +323,8 @@ github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc= github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8= github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ= +github.com/urfave/cli/v3 v3.0.0-alpha9 h1:P0RMy5fQm1AslQS+XCmy9UknDXctOmG/q/FZkUFnJSo= +github.com/urfave/cli/v3 v3.0.0-alpha9/go.mod h1:0kK/RUFHyh+yIKSfWxwheGndfnrvYSmYFVeKCh03ZUc= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= diff --git a/internal/bootstrap/cli.go b/internal/bootstrap/cli.go index 9d634690..1b6e6519 100644 --- a/internal/bootstrap/cli.go +++ b/internal/bootstrap/cli.go @@ -1,26 +1,48 @@ package bootstrap import ( + "context" "fmt" "os" + "strings" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" "github.com/TheTNB/panel/internal/panel" "github.com/TheTNB/panel/internal/route" ) func initCli() { - app := &cli.App{ - Name: "panel-cli", - HelpName: fmt.Sprintf("耗子面板 %s", panel.Version), - Usage: "命令行工具", - UsageText: "panel-cli [global options] command [command options] [arguments...]", - HideVersion: true, - Commands: route.Cli(), - } + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "NAME", "名称") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "USAGE", "用法") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "VERSION", "版本") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "DESCRIPTION", "描述") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "AUTHOR", "作者") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "COMMANDS", "命令") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "GLOBAL OPTIONS", "全局选项") + cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "COPYRIGHT", "版权") + cli.CommandHelpTemplate = strings.ReplaceAll(cli.CommandHelpTemplate, "NAME", "名称") + cli.CommandHelpTemplate = strings.ReplaceAll(cli.CommandHelpTemplate, "USAGE", "用法") + cli.CommandHelpTemplate = strings.ReplaceAll(cli.CommandHelpTemplate, "CATEGORY", "分类") + cli.CommandHelpTemplate = strings.ReplaceAll(cli.CommandHelpTemplate, "DESCRIPTION", "描述") + cli.CommandHelpTemplate = strings.ReplaceAll(cli.CommandHelpTemplate, "OPTIONS", "选项") + cli.SubcommandHelpTemplate = strings.ReplaceAll(cli.SubcommandHelpTemplate, "NAME", "名称") + cli.SubcommandHelpTemplate = strings.ReplaceAll(cli.SubcommandHelpTemplate, "USAGE", "用法") + cli.SubcommandHelpTemplate = strings.ReplaceAll(cli.SubcommandHelpTemplate, "DESCRIPTION", "描述") + cli.SubcommandHelpTemplate = strings.ReplaceAll(cli.SubcommandHelpTemplate, "COMMANDS", "命令") + cli.SubcommandHelpTemplate = strings.ReplaceAll(cli.SubcommandHelpTemplate, "OPTIONS", "选项") - if err := app.Run(os.Args); err != nil { + cli.RootCommandHelpTemplate += "\n官网:https://panel.haozi.net" + cli.RootCommandHelpTemplate += "\n论坛:https://tom.moe" + cli.RootCommandHelpTemplate += "\nQ群:12370907\n" + + cmd := &cli.Command{ + Name: "panel-cli", + Usage: "耗子面板命令行工具", + Version: panel.Version, + Commands: route.Cli(), + } + if err := cmd.Run(context.Background(), os.Args); err != nil { panic(fmt.Sprintf("failed to run cli: %v", err)) } } diff --git a/internal/route/cli.go b/internal/route/cli.go index febbd9d5..8cbc389c 100644 --- a/internal/route/cli.go +++ b/internal/route/cli.go @@ -1,19 +1,281 @@ package route import ( - "github.com/urfave/cli/v2" + "context" + "fmt" - "github.com/TheTNB/panel/internal/service" + "github.com/urfave/cli/v3" ) func Cli() []*cli.Command { - cliService := service.NewCliService() + //cliService := service.NewCliService() return []*cli.Command{ { - Name: "test", - Aliases: []string{"t"}, - Usage: "print a test message", - Action: cliService.Test, + Name: "restart", + Usage: "重启面板服务", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "stop", + Usage: "停止面板服务", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "start", + Usage: "启动面板服务", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "update", + Usage: "升级面板主程序", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "update-cli", + Usage: "升级面板命令行工具", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "info", + Usage: "输出面板基本信息", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "user", + Usage: "操作面板用户", + Commands: []*cli.Command{ + { + Name: "list", + Usage: "列出所有用户", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "username", + Usage: "修改用户名", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "password", + Usage: "修改用户密码", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + }, + }, + { + Name: "https", + Usage: "操作面板HTTPS", + Commands: []*cli.Command{ + { + Name: "on", + Usage: "开启HTTPS", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "off", + Usage: "关闭HTTPS", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + }, + }, + { + Name: "entrance", + Usage: "操作面板访问入口", + Commands: []*cli.Command{ + { + Name: "on", + Usage: "开启访问入口", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "off", + Usage: "关闭访问入口", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + }, + }, + { + Name: "port", + Usage: "修改面板端口", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "website", + Usage: "网站管理", + Commands: []*cli.Command{ + { + Name: "add", + Usage: "创建新站点", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("new task template: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "remove", + Usage: "移除站点", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("removed task template: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "delete", + Usage: "删除站点(包括站点目录、同名数据库)", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("removed task template: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "write", + Usage: "写入网站数据(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("removed task template: ", cmd.Args().First()) + return nil + }, + }, + }, + }, + { + Name: "backup", + Usage: "备份数据", + Commands: []*cli.Command{ + { + Name: "website", + Usage: "备份网站", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("new task template: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "database", + Usage: "备份数据库", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("removed task template: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "panel", + Usage: "备份面板", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("removed task template: ", cmd.Args().First()) + return nil + }, + }, + }, + }, + { + Name: "cutoff", + Usage: "日志切割", + Commands: []*cli.Command{ + { + Name: "website", + Usage: "网站", + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("new task template: ", cmd.Args().First()) + return nil + }, + }, + }, + }, + { + Name: "writeApp", + Usage: "添加面板应用标记(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "removeApp", + Usage: "移除面板应用标记(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "cleanTask", + Usage: "清理面板任务队列(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "writeSetting", + Usage: "写入面板设置(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "removeSetting", + Usage: "移除面板设置(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("completed task: ", cmd.Args().First()) + return nil + }, + }, + { + Name: "init", + Usage: "初始化面板(仅限指导下使用)", + Hidden: true, + Action: func(ctx context.Context, cmd *cli.Command) error { + fmt.Println("added task: ", cmd.Args().First()) + return nil + }, }, } }