0

我有以下命令+子命令:

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?

4

1 回答 1

0

go-cobra 命令和子命令旨在单独运行。有一些 hacky 方法可以运行多个,但通常这意味着需要为您的 args 提供特殊格式并自己处理批处理运行。请参阅https://github.com/spf13/cobra/issues/726上的讨论,以获取一种方法的示例以及一些相关问题的指针。

于 2020-09-30T23:54:33.103 回答