环境变量设置
export GOBIN=$GOPATH/binexport PATH=$GOBIN:$PATHexport GOPROXY=https://goproxy.cn/,direct
装置依赖
go get -u github.com/spf13/[email protected]go get -u github.com/spf13/[email protected]
Cobra我的项目流程
参考
Create rootCmdCreate your main.goCreate additional commandsReturning and handling errors# 为了疾速实现我的项目&上班,倡议应用cobra-cli来实现以上步骤# 上面的演示也是用cobra-cli来实现
应用 Cobra CLI 初始化我的项目
参考
mkdir myapp && cd myapp && go mod init myappcobra-cli initmyapp/ $ tree .├── cmd│ └── root.go├── go.mod├── go.sum├── LICENSE└── main.gomyapp/ $ go run main.go 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.
// // cmd/root.go/*Copyright © 2022 NAME HERE <EMAIL ADDRESS>*/package cmdimport ( "os" "github.com/spf13/cobra")// rootCmd represents the base command when called without any subcommandsvar rootCmd = &cobra.Command{ Use: "myapp", 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() { 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/.myapp.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")}
我的项目增加command
$ cobra-cli add config [10:38:42]config created at /home/project/myapp$ ls cmd config.go root.go
// cmd/config.go/*Copyright © 2022 NAME HERE <EMAIL ADDRESS>*/package cmdimport ( "fmt" "github.com/spf13/cobra")// configCmd represents the config commandvar configCmd = &cobra.Command{ Use: "config", 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("config called") },}func init() { rootCmd.AddCommand(configCmd) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: // configCmd.PersistentFlags().String("foo", "", "A help for foo") // Cobra supports local flags which will only run when this command // is called directly, e.g.: // configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")}