https://github.com/spf13/cobra.git
使用golang可以很方便把代码编译成二进制,但是如何写出类似linux工具那样,符合posix标准的工具呢?答案是cobra。
kubectl使用cobra框架实现。
快速开始
cobra-cli
go install github.com/spf13/cobra-cli@latest
$GOPATH/bin$GOPATH/bin
运行下面命令快速初始化:
cobra-cli init
会生成如下结构:
- main.go
+ cmd
- root.go
此时就已经可以运行项目简单的试试效果:
$ go run main.go -h
A longer description that spans multiple lines and likely contains
examples and usage of using your application. 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.
Usage:
cobra-test [flags]
cobra-test [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
Flags:
-h, --help help for cobra-test
-t, --toggle Help message for toggle
添加一个子命令
比如:
git clone xxx
clone
serve.go
cobra-cli add serve
添加开关参数
- serve.go
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
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("serve called")
// 位置参数
fmt.Println(args)
// 开关参数
fmt.Println(cmd.Flags().GetBool("toggle"))
},
}
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
args代表位置参数,toggle代表开关参数
$ go run main.go serve -t hello
serve called
[hello]
true <nil>
长参数
git clone --config /etc/config.yaml
--config /etc/config.yaml
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
// config file path
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cobra-test",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. 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.`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
// 这里根据参数获取情况,执行相关逻辑
fmt.Println(cfgFile)
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra-test.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// 这一行定义类似 go run main.go --config xxx.yaml
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
}