Cobra-CLI将所有参数和标志传递给可执行文件

发布于 2025-02-09 07:17:31 字数 1209 浏览 2 评论 0 原文

我为自己的东西有一个眼镜蛇CLI。现在,我想添加常用的可执行文件 eg kubectl calicoctl 作为子命令,将消耗所有参数和标志,例如

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

复制COBRA Project

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

并添加一个subcommand eg 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 命令 eg 进行运行。 kubectl获取豆荚。但是,当添加标志 eg 时,它会失败。 kubectl获取豆荚-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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2025-02-16 07:17:31

之后传递标志 - 。双仪表( - )用于表示命令选项的结尾。在我们的情况下,需要区分传递给 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 (
    "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)
}

Then tried:

$ 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

Adding more flags:

$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文