在F盘创建文件夹cobra-started
1、使用mod对go项目进行管理
go mod init cobra-started
参考:https://github.com/spf13/cobra-cli/blob/main/README.md
1、设置 GOPROXY代理
PS F:\k8s\cobra-started> $env:GOPROXY = "https://proxy.golang.com.cn,direct"
2、下载cobra-cli@latest
PS F:\k8s\cobra-started> go get github.com/spf13/cobra-cli@latest go: downloading github.com/spf13/cobra-cli v1.3.0
go: downloading github.com/spf13/viper v1.10.1
go: downloading github.com/spf13/cobra v1.3.0
go: downloading github.com/mitchellh/mapstructure v1.4.3
go: downloading github.com/spf13/cast v1.4.1
go: downloading github.com/fsnotify/fsnotify v1.5.1
go: downloading github.com/magiconair/properties v1.8.5
go: downloading github.com/spf13/jwalterweatherman v1.1.0
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading gopkg.in/ini.v1 v1.66.2
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/pelletier/go-toml v1.9.4
go: downloading golang.org/x/sys v0.0.0-20211210111614-af8b64212486
go get: installing executables with 'go get' in module mode is deprecated.
To adjust and download dependencies of the current module, use 'go get -d'.
To install using requirements of the current module, use 'go install'.
To install ignoring the current module, use 'go install' with a version,
like 'go install example.com/cmd@latest'.
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
go get: added github.com/fsnotify/fsnotify v1.5.1
go get: added github.com/hashicorp/hcl v1.0.0
go get: added github.com/inconshreveable/mousetrap v1.0.0
go get: added github.com/magiconair/properties v1.8.5
go get: added github.com/mitchellh/mapstructure v1.4.3
go get: added github.com/pelletier/go-toml v1.9.4
go get: added github.com/spf13/afero v1.6.0
go get: added github.com/spf13/cast v1.4.1
go get: added github.com/spf13/cobra v1.3.0
go get: added github.com/spf13/cobra-cli v1.3.0
go get: added github.com/spf13/jwalterweatherman v1.1.0
go get: added github.com/spf13/pflag v1.0.5
go get: added github.com/spf13/viper v1.10.1
go get: added github.com/subosito/gotenv v1.2.0
go get: added golang.org/x/sys v0.0.0-20211210111614-af8b64212486
go get: added golang.org/x/text v0.3.7
go get: added gopkg.in/ini.v1 v1.66.2
go get: added gopkg.in/yaml.v2 v2.4.0
3、从$GOPATH/bin目录下把cobra-cli.exe命令拷贝到F:\k8s\cobra-started目录下
4、执行 .\cobra-cli init生成代码结构
PS F:\k8s\cobra-started> .\cobra-cli init Your Cobra application is ready at
F:\k8s\cobra-started
5、查看生成的代码结构
PS F:\k8s\cobra-started> ls
目录: F:\k8s\cobra-started
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2022/4/2 9:17 cmd
-a---- 2022/4/2 9:14 8210944 cobra-cli.exe
-a---- 2022/4/2 9:17 891 go.mod
-a---- 2022/4/2 9:17 77739 go.sum
-a---- 2022/4/2 9:17 0 LICENSE
-a---- 2022/4/2 9:17 124 main.go
6、生成的文件
main.go
package main
import "cobra-started/cmd"
func main() {
cmd.Execute()
}
cmd/root.go
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cobra-started",
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) { },
}
// 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-started.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")
}