装置cobra

go get -v github.com/spf13/cobra/cobra

切换到GOPATH的src目录下并创立一个新文件夹:demo

cd $GOPATH/srcmkdir democd demo

初始cobra

$  ../../bin/cobra Cobra is a CLI library for Go that empowers applications.This application is a tool to generate the needed filesto quickly create a Cobra application.Usage:  cobra [command]Available Commands:  add         Add a command to a Cobra Application  help        Help about any command  init        Initialize a Cobra ApplicationFlags:  -a, --author string    author name for copyright attribution (default "YOUR NAME")      --config string    config file (default is $HOME/.cobra.yaml)  -h, --help             help for cobra  -l, --license string   name of license for the project      --viper            use Viper for configuration (default true)Use "cobra [command] --help" for more information about a command.

能够看到cobra反对两个命令,一个是init, 一个是add,其中init是初始化一个cobra工程,add是给工程中增加一个子命令

初始化该我的项目:

$ ../../bin/cobra init --pkg-name demo

执行完上述命令后会生成如下几个文件及文件夹

$ tree .├── cmd│   └── root.go├── LICENSE└── main.go1 directory, 3 files

main.go

package mainimport "imgctl/cmd"func main() {    cmd.Execute()}

cmd/root.go

package cmdimport (  "fmt"  "os"  "github.com/spf13/cobra"  homedir "github.com/mitchellh/go-homedir"  "github.com/spf13/viper")var cfgFile string// rootCmd represents the base command when called without any subcommandsvar rootCmd = &cobra.Command{  Use:   "cobra-demo",  Short: "A brief description of your application",  Long: `A longer description that spans multiple lines and likely containsexamples 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 filesto 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) { },}// 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() {  if err := rootCmd.Execute(); err != nil {    fmt.Println(err)    os.Exit(1)  }}func init() {  cobra.OnInitialize(initConfig)  // 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-demo.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")}// initConfig reads in config file and ENV variables if set.func initConfig() {  if cfgFile != "" {    // Use config file from the flag.    viper.SetConfigFile(cfgFile)  } else {    // Find home directory.    home, err := homedir.Dir()    if err != nil {      fmt.Println(err)      os.Exit(1)    }    // Search config in home directory with name ".cobra-demo" (without extension).    viper.AddConfigPath(home)    viper.SetConfigName(".cobra-demo")  }  viper.AutomaticEnv() // read in environment variables that match  // If a config file is found, read it in.  if err := viper.ReadInConfig(); err == nil {    fmt.Println("Using config file:", viper.ConfigFileUsed())  }}

如果要实现一个没有子命令的CLI工具,那么由cobra生成程序的操作就完结了,因为cobra的所有命令都是通过cobra.Command这个构造体实现的,因而这里就须要对root.go文件中的RootCmd进行批改:

cmd/root.go

package cmdimport (  "fmt"  "os"  "github.com/spf13/cobra"  homedir "github.com/mitchellh/go-homedir"  "github.com/spf13/viper")var cfgFile stringvar name stringvar age int// rootCmd represents the base command when called without any subcommandsvar rootCmd = &cobra.Command{  Use:   "cobra-demo",  Short: "A brief description of your application",  Long: `A longer description that spans multiple lines and likely containsexamples 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 filesto 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) { },}// RootCmd represents the base command when called without any subcommandsvar RootCmd = &cobra.Command{    Use:   "demo",    Short: "A test demo",    Long:  `Demo is a test appcation for print things`,    // Uncomment the following line if your bare application    // has an action associated with it:    Run: func(cmd *cobra.Command, args []string) {        if len(name) == 0 {            cmd.Help()            return        }        fmt.Println(name, age)    },}// 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() {  if err := rootCmd.Execute(); err != nil {    fmt.Println(err)    os.Exit(1)  }}func init() {  cobra.OnInitialize(initConfig)  // 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-demo.yaml)")  rootCmd.Flags().StringVarP(&name, "name", "n", "", "person's name")  rootCmd.Flags().IntVarP(&age, "age", "a", 0, "person's age")  // 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")}// initConfig reads in config file and ENV variables if set.func initConfig() {  if cfgFile != "" {    // Use config file from the flag.    viper.SetConfigFile(cfgFile)  } else {    // Find home directory.    home, err := homedir.Dir()    if err != nil {      fmt.Println(err)      os.Exit(1)    }    // Search config in home directory with name ".cobra-demo" (without extension).    viper.AddConfigPath(home)    viper.SetConfigName(".cobra-demo")  }  viper.AutomaticEnv() // read in environment variables that match  // If a config file is found, read it in.  if err := viper.ReadInConfig(); err == nil {    fmt.Println("Using config file:", viper.ConfigFileUsed())  }}

到此咱们的demo性能就编码实现了,上面编译运行

解决依赖

go mod initgo mod vendor

运行demo

$ go run main.goDemo is a test appcation for print thingsUsage:  demo [flags]Flags:  -a, --age int       person's age  -h, --help          help for demo  -n, --name string   person's name

增加命令

$ ../../bin/cobra add  [cmdname] # 执行后会在cmd目录下生成一个cmdname.go的文件,具体处理函数在该文件中编辑$ ../../bin/cobra add server$ tree.├── cmd│   ├── root.go│   └── server.go # rootCmd的子命令├── LICENSE└── main.go1 directory, 4 files

cmd/server.go

package cmdimport (        "fmt"        "github.com/spf13/cobra")// serverCmd represents the server commandvar serverCmd = &cobra.Command{        Use:   "server",        Short: "A brief description of your command",        Long: `A longer description that spans multiple lines and likely contains examplesand 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 filesto quickly create a Cobra application.`,        Run: func(cmd *cobra.Command, args []string) {                fmt.Println("server called")        },}func init() {        rootCmd.AddCommand(serverCmd)        // Here you will define your flags and configuration settings.        // Cobra supports Persistent Flags which will work for this command        // and all subcommands, e.g.:        // serverCmd.PersistentFlags().String("foo", "", "A help for foo")        // Cobra supports local flags which will only run when this command        // is called directly, e.g.:        // serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")}$ ../../bin/cobra add create -p "serverCmd" #执行该命令是为server子命令再创立一级子命令,-p --parent$ tree.├── cmd│   ├── create.go # serverCmd的子命令│   ├── root.go│   └── server.go├── LICENSE└── main.go1 directory, 5 files

cmd/create.go

package cmdimport (        "fmt"        "github.com/spf13/cobra")// creatCmd represents the creat commandvar creatCmd = &cobra.Command{        Use:   "creat",        Short: "A brief description of your command",        Long: `A longer description that spans multiple lines and likely contains examplesand 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 filesto quickly create a Cobra application.`,        Run: func(cmd *cobra.Command, args []string) {                fmt.Println("creat called")        },}func init() {        serverCmd.AddCommand(creatCmd)        // Here you will define your flags and configuration settings.        // Cobra supports Persistent Flags which will work for this command        // and all subcommands, e.g.:        // creatCmd.PersistentFlags().String("foo", "", "A help for foo")        // Cobra supports local flags which will only run when this command        // is called directly, e.g.:        // creatCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")}