# 创建子命令server ➜ cobra add server server created at /Users/liuqh/ProjectItem/GoItem/go-cli
2. 查看创建子命令内容
文件位置:cmd/server.go
package cmd
import ( "fmt" "github.com/spf13/cobra" ) // serverCmd represents the server command var serverCmd = &cobra.Command{ Use: "server", Short: "A brief description of your command", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("server called") }, } funcinit() { rootCmd.AddCommand(serverCmd) }
3. 编辑子命令内容
文件位置:cmd/server.go
package cmd
import ( "fmt" "github.com/gin-gonic/gin" "github.com/spf13/cobra" "os" ) // serverCmd represents the server command var ( serverCmd = &cobra.Command{ Use: "server", Short: "启动http服务,使用方法: app server --port=?", Run: func(cmd *cobra.Command, args []string) { if port == "" { fmt.Println("port不能为空!") os.Exit(-1) } engine := gin.Default() _ = engine.Run(":" + port) }, } //接收端口号 port string ) funcinit() { // 添加命令 rootCmd.AddCommand(serverCmd) // 接收参数port serverCmd.Flags().StringVar(&port, "port", "", "端口号") }
3.3 编译运行
1. 编译
# 编译(编译后的文件名为: app) ➜ go build -o app .
2. 运行(不带参数)
➜ ./app 学习使用Cobra,开发cli项目, - app: 指的是编译后的文件名。
Usage: app [command]
Available Commands: completion generate the autocompletion script for the specified shell help Help about any command server 启动http服务,使用方法: app server --port=?
Flags: -h, --helphelpfor app --version string 版本
Use "app [command] --help"for more information about a command.
3. 查看具体子命令
# 查看server使用信息 ➜ ./app server -h 启动http服务,使用方法: app server --port=?
Usage: app server [flags]
Flags: -h, --helphelpfor server --port string 端口号
Global Flags: --version string 版本
4. 运行子命令
# 不传必填参数时 ➜ ./app server port不能为空! # 传参数 ➜ ./app server --port 8090 [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in"debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)
type AppConfig struct { App app `yaml:"app"` MySql mysql `yaml:"mysql"` } type app struct { Version string`yaml:"version"` Author string`yaml:"author"` Port string`yaml:"port"` } type mysql struct { Host string`yaml:"host"` DataBase string`yaml:"data_base"` User string`yaml:"user"` Password string`yaml:"password"` }
# 默认启动http ➜ ./cli server &{App:{Version:v1.0.0 Author:刘庆辉 Port:8080} MySql:{Host:127.0.0.1 DataBase: User:root Password:root}} [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in"debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] Listening and serving HTTP on :8080
# 指定配置文件启动 ➜ ./cli server --config=./local.yaml &{App:{Version:v1.0.2 Author:刘庆辉 Port:8081} MySql:{Host:192.168.0.10 DataBase: User:root Password:root}} [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in"debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)