问题描述
我有以下命令+子命令:
aws.go
// s3Cmd represents the out command
var s3Cmd = &cobra.Command{
Use: "s3",Short: "Uploads saved tarballs to an s3 bucket in aws",Long: `Uploads files to S3 using the credentials passed as arguments
s3Bucket and the aws key and secret.`,RunE: func(cmd *cobra.Command,args []string) error {
// some logic
},}
func init() {
outCmd.AddCommand(s3Cmd)
}
out.go
// outCmd represents the out command
var outCmd = &cobra.Command{
Use: "out",Short: "Consumes data out from RabbitMQ and stores to tarballs",Long: `Select your output directory and batchsize of the tarballs.
When there are no more messages in the queue,press CTRL + c,to interrupt
the consumption and save the last message buffers.`,args []string) error {
//logic
},}
func init() {
RootCmd.AddCommand(outCmd)
}
当我执行go run main.go out --args s3 --args
上面的代码运行s3Command内部的逻辑,但不运行outCmd内部的逻辑,有没有办法先运行outCommand逻辑,然后先运行s3Cmd?
解决方法
go-cobra命令和子命令应单独运行。有多种方法可以运行多个,但是通常这意味着需要对args使用特殊格式并自行处理运行。请参阅https://github.com/spf13/cobra/issues/726上的讨论,以获取一种实现方法的示例以及指向一些相关问题的指针。