cobra-cli将所有参数和标志传递给可执行文件。

huangapple go评论77阅读模式
英文:

cobra-cli pass all arguments and flags to an executable

问题

我有一个用于自己的东西的 cobra CLI。现在我想添加常用的可执行文件,例如 kubectlcalicoctl,作为子命令,它们将接受所有的参数和标志,例如:

mywrapper kubectl get all --all-namespaces
mywrapper kubectl create deployment nginx --image=nginx --port=80

重新创建 cobra 项目:

mkdir mywrapper; cd mywrapper; go mod init mywrapper; cobra-cli init .

然后添加一个子命令,例如 kubectl

cobra-cli add kubectl 

然后在 ./cmd/kubectl.go 中填充以下内容:

package cmd

import (
	"fmt"
	"os/exec"
	"strings"

	"github.com/spf13/cobra"
)

var kubectlCmd = &cobra.Command{
	Use:   "kubectl",
	Short: "run kubectl",
	Run: func(cmd *cobra.Command, args []string) {
		out, err := exec.Command("/bin/bash", "-c", fmt.Sprintf("kubectl %v", strings.Join(args, " "))).Output()
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(string(out))
	},
}

func init() {
	rootCmd.AddCommand(kubectlCmd)
}

现在我可以运行 kubectl 命令,例如 go run . kubectl get pods。但是当添加标志时,例如 go run . kubectl get pods --selector app=nginx,它会失败。

英文:

I have a cobra CLI for my own stuff. Now I want to add commonly used executables e.g. kubectl, calicoctl as subcommands which will consume all arguments and flags like

mywrapper kubectl get all --all-namespaces
mywrapper kubectl create deployment nginx --image=nginx --port=80

Reproduce cobra project

mkdir mywrapper; cd mywrapper; go mod init mywrapper; cobra-cli init .

And add a subcommand e.g. kubectl

cobra-cli add kubectl 

Then populate ./cmd/kubectl.go with

package cmd

import (
	"fmt"
    "os/exec"
    "strings"

	"github.com/spf13/cobra"
)

var kubectlCmd = &cobra.Command{
	Use:   "kubectl",
	Short: "run kubectl",
	Run: func(cmd *cobra.Command, args []string) {
        out, err := exec.Command("/bin/bash", "-c", fmt.Sprintf("kubectl %v", strings.Join(args, " "))).Output()
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(string(out))
	},
}

func init() {
	rootCmd.AddCommand(kubectlCmd)
}

I can now run kubectl command e.g. go run . kubectl get pods. But it fails when flags are added e.g. go run . kubectl get pods --selector app=nginx

答案1

得分: 3

--之后传递你的标志。双破折号(--)用于表示命令选项的结束。在我们的情况下,这是为了区分传递给go的标志和那些不是的标志。双破折号之后的所有内容都不会被视为go的标志。

我尝试了使用gcloud:

package cmd

import (
	"fmt"
	"os/exec"

	"github.com/spf13/cobra"
)

var gcloudCmd = &cobra.Command{
	Use:   "gcloud",
	Short: "run gcloud",
	Run: func(cmd *cobra.Command, args []string) {
		out, err := exec.Command("gcloud", args...).Output()
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(string(out))
	},
}

func init() {
	rootCmd.AddCommand(gcloudCmd)
}

然后尝试了以下命令:

$ go run . gcloud compute regions list -- --filter="id<1250"

NAME          CPUS  DISKS_GB  ADDRESSES  RESERVED_ADDRESSES  STATUS  TURNDOWN_DATE
asia-east1    0/24  0/4096    0/8        0/8                 UP
europe-west1  0/24  0/4096    0/8        0/8                 UP
us-central1   0/24  0/4096    0/8        0/8                 UP
us-east1      0/24  0/4096    0/8        0/8                 UP
us-west1      0/24  0/4096    0/8        0/8                 UP

添加更多的标志:

$ go run . gcloud compute regions list -- --filter="id<1250" --format="table(name,id)"

NAME          ID
asia-east1    1220
europe-west1  1100
us-central1   1000
us-east1      1230
us-west1      1210
英文:

Pass your flags after the --. A double dash (--) is used to signify the end of command options. In our case it is required to distinguish between the flags passed to go and those that aren't. Everything after the double dash wont be considered to be go's flags.

I tried with gcloud:

package cmd

import (
	&quot;fmt&quot;
	&quot;os/exec&quot;

	&quot;github.com/spf13/cobra&quot;
)

var gcloudCmd = &amp;cobra.Command{
	Use:   &quot;gcloud&quot;,
	Short: &quot;run gcloud&quot;,
	Run: func(cmd *cobra.Command, args []string) {
		out, err := exec.Command(&quot;gcloud&quot;, args...).Output()
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(string(out))
	},
}

func init() {
	rootCmd.AddCommand(gcloudCmd)
}

Then tried:

$ go run . gcloud compute regions list -- --filter=&quot;id&lt;1250&quot;

NAME          CPUS  DISKS_GB  ADDRESSES  RESERVED_ADDRESSES  STATUS  TURNDOWN_DATE
asia-east1    0/24  0/4096    0/8        0/8                 UP
europe-west1  0/24  0/4096    0/8        0/8                 UP
us-central1   0/24  0/4096    0/8        0/8                 UP
us-east1      0/24  0/4096    0/8        0/8                 UP
us-west1      0/24  0/4096    0/8        0/8                 UP

Adding more flags:

$ go run . gcloud compute regions list -- --filter=&quot;id&lt;1250&quot; --format=&quot;table(name,id)&quot;

NAME          ID
asia-east1    1220
europe-west1  1100
us-central1   1000
us-east1      1230
us-west1      1210

huangapple
  • 本文由 发表于 2022年6月22日 08:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72708535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定