From 275bbc17cc83ac7b1f8130ea108ef85f92161782 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Wed, 14 Dec 2022 21:03:47 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E5=A2=9E=E5=8A=A0=201panel=20cli?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/cmd/backup.go | 20 ++++++++++++++++++++ cmd/server/cmd/restore.go | 19 +++++++++++++++++++ cmd/server/cmd/root.go | 34 ++++++++++++++++++++++++++++++++++ cmd/server/main.go | 9 +++++++-- go.mod | 4 +++- go.sum | 4 ++++ 6 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 cmd/server/cmd/backup.go create mode 100644 cmd/server/cmd/restore.go create mode 100644 cmd/server/cmd/root.go diff --git a/cmd/server/cmd/backup.go b/cmd/server/cmd/backup.go new file mode 100644 index 000000000..3046c49c2 --- /dev/null +++ b/cmd/server/cmd/backup.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" +) + +func init() { + backupCmd.Flags().StringVarP(&configPath, "dir", "d", "/opt/backup", "备份目录") + RootCmd.AddCommand(backupCmd) +} + +var backupCmd = &cobra.Command{ + Use: "backup", + Short: "备份1Panel", + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println("备份成功") + return nil + }, +} diff --git a/cmd/server/cmd/restore.go b/cmd/server/cmd/restore.go new file mode 100644 index 000000000..e9099fdca --- /dev/null +++ b/cmd/server/cmd/restore.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" +) + +func init() { + RootCmd.AddCommand(restoreCmd) +} + +var restoreCmd = &cobra.Command{ + Use: "restore", + Short: "回滚1Panel", + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println("回滚成功") + return nil + }, +} diff --git a/cmd/server/cmd/root.go b/cmd/server/cmd/root.go new file mode 100644 index 000000000..f8a4cae8d --- /dev/null +++ b/cmd/server/cmd/root.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "github.com/1Panel-dev/1Panel/backend/server" + "github.com/spf13/cobra" +) + +var ( + configPath string +) + +func init() { + RootCmd.Flags().BoolP("run", "r", false, "运行面板") + RootCmd.Flags().StringVarP(&configPath, "config", "c", "/opt/1panel/conf/app.yml", "配置文件路径") +} + +var RootCmd = &cobra.Command{ + Use: "1panel", + Short: "1Panel ,一款现代化的 Linux 面板", + Long: `欢迎使用 1Panel 面板 +github地址: https://github.com/1Panel-dev/1Panel +你可以使用如下命令操作1Panel +例如: 1panel -r 启动1Panel服务 +1panel backup -d /some/path 备份1Panel +你也可以使用 1panel --help 查看帮助信息 +或者使用 1panel xx --help 查看具体命令的帮助信息`, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 || args[0] == "run" || args[0] == "r" { + server.Start() + return nil + } + return nil + }, +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 6b21615e2..d879560ec 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,8 +1,10 @@ package main import ( - "github.com/1Panel-dev/1Panel/backend/server" + "fmt" + "github.com/1Panel-dev/1Panel/cmd/server/cmd" _ "github.com/1Panel-dev/1Panel/cmd/server/docs" + "os" ) // @title 1Panel @@ -18,5 +20,8 @@ import ( //go:generate swag init -o ./docs -d ../../backend/app/api/v1 -g ../../../../cmd/server/main.go func main() { - server.Start() + if err := cmd.RootCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } } diff --git a/go.mod b/go.mod index f31c8aa9a..bb8e65312 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/sirupsen/logrus v1.9.0 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/spf13/afero v1.8.2 + github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.12.0 github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a github.com/swaggo/gin-swagger v1.5.1 @@ -100,6 +101,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/imdario/mergo v0.3.13 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect @@ -115,7 +117,7 @@ require ( github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/mattn/go-shellwords v1.0.12 // indirect - github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect + github.com/mattn/go-sqlite3 v1.14.16 // indirect github.com/miekg/dns v1.1.50 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/sha256-simd v1.0.0 // indirect diff --git a/go.sum b/go.sum index 7ddf0a858..827ba5036 100644 --- a/go.sum +++ b/go.sum @@ -573,6 +573,7 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= @@ -681,6 +682,8 @@ github.com/mattn/go-shellwords v1.0.6/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vq github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -895,6 +898,7 @@ github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155 github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=