Golang------cobra 客官°小女子只卖身不卖艺 2022-12-29 09:37 145阅读 0赞 # 安装 # go get -u github.com/spf13/cobra/cobra 在`pkg\mod\github.com\spf13\cobra@v1.1.1\cobra`中执行`go build` 把生成的`cobra.exe`放到go的bin目录下,便于全局执行命令 新建一个项目文件夹,在文件夹里面执行 cobra init --pkg-name CobraDemo ![在这里插入图片描述][2020122120181562.png] 默认会创建一个cmd文件夹里面有一个root.go文件 ![文件目录结构][20201221201944393.png] 我添加了一个go.mod文件用于管理依赖 go.mod module CobraDemo go 1.15 require ( github.com/mitchellh/go-homedir v1.1.0 github.com/spf13/cobra v1.1.1 github.com/spf13/viper v1.7.0 ) root.go /* Copyright © 2020 NAME HERE <EMAIL ADDRESS> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package cmd import ( "fmt" "github.com/spf13/cobra" "os" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "CobraDemo", 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: // todo: 实现下面这个方法,用于业务处理 Run: func(cmd *cobra.Command, args []string) { fmt.Println("cobra!!!!!") }, } // 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/.CobraDemo.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 ".CobraDemo" (without extension). viper.AddConfigPath(home) viper.SetConfigName(".CobraDemo") } 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()) } } ![在这里插入图片描述][2020122120232248.png] ## 新增version命令 ## cobra add version ![在这里插入图片描述][20201221202147566.png] ![在这里插入图片描述][20201221202155802.png] ![在这里插入图片描述][20201221202404264.png] [2020122120181562.png]: /images/20221120/6ac59314d53e4d2d9de9073c199bae3f.png [20201221201944393.png]: /images/20221120/70309af7c4944ff9a6cb81a9b0d5b2c8.png [2020122120232248.png]: /images/20221120/d0e0fd2d8e9b4efa9f9763df7f417e25.png [20201221202147566.png]: /images/20221120/19fa88234efa484d80bbc62936f61b07.png [20201221202155802.png]: /images/20221120/4890fcfb471241dd8983e95c9f440aa5.png [20201221202404264.png]: /images/20221120/15a01dca3390470e8787744c146d6b67.png
还没有评论,来说两句吧...