我有以下命令+子命令:
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.`,
RunE: func(cmd *cobra.Command, args []string) error {
//logic
},
}
func init() {
RootCmd.AddCommand(outCmd)
}
当我执行go run main.go out --args s3 --args
时
上面运行了s3Command内部的逻辑,但没有运行outCmd内部的内容,有没有办法先运行outCommand逻辑,然后再运行s3Cmd?
go cobra命令和子命令是单独运行的。有一些巧妙的方法可以运行多个,但一般来说,这意味着需要对参数使用特殊格式,并自己处理批处理运行。请参阅上的讨论https://github.com/spf13/cobra/issues/726以获取一种方法的示例,并指出一些相关问题。