Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release-1.31] Add ctr to shell completion #6731

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions pkg/cli/cmds/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var (
Name: "crictl",
Usage: "(crictl) export crictl config file",
},
&cli.BoolFlag{
Name: "ctr",
Usage: "(ctr) export containerd sock file",
},
}

k3sCompletionBase = mustCmdFromK3S(cmds.NewCompletionCommand(Run), K3SFlagSet{
Expand Down Expand Up @@ -46,23 +50,30 @@ func isCrictlSet(crictl bool) string {
return ""
}

func isCtrSet(ctr bool) string {
if ctr {
return " --ctr"
}
return ""
}

func Run(ctx *cli.Context) error {
if ctx.NArg() < 1 {
return fmt.Errorf("must provide a valid SHELL argument")
}
shell := ctx.Args()[0]
completetionScript, err := genCompletionScript(shell, ctx.Bool("kubectl"), ctx.Bool("crictl"))
completetionScript, err := genCompletionScript(shell, ctx.Bool("kubectl"), ctx.Bool("crictl"), ctx.Bool("ctr"))
if err != nil {
return err
}
if ctx.Bool("i") {
return writeToRC(shell, ctx.Bool("kubectl"), ctx.Bool("crictl"))
return writeToRC(shell, ctx.Bool("kubectl"), ctx.Bool("crictl"), ctx.Bool("ctr"))
}
fmt.Println(completetionScript)
return nil
}

func genCompletionScript(shell string, kubectl, crictl bool) (string, error) {
func genCompletionScript(shell string, kubectl, crictl, ctr bool) (string, error) {
var completionScript string
if shell == "bash" {
completionScript = fmt.Sprintf(`#! /bin/bash
Expand Down Expand Up @@ -120,13 +131,19 @@ export KUBECONFIG=/etc/rancher/rke2/rke2.yaml
if crictl {
completionScript = fmt.Sprintf(`%s
export CRI_CONFIG_FILE=/var/lib/rancher/rke2/agent/etc/crictl.yaml
`, completionScript)
}

if ctr {
completionScript = fmt.Sprintf(`%s
export CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock
`, completionScript)
}

return completionScript, nil
}

func writeToRC(shell string, kubectl, crictl bool) error {
func writeToRC(shell string, kubectl, crictl, ctr bool) error {
rcFileName := ""
if shell == "bash" {
rcFileName = "/.bashrc"
Expand All @@ -144,7 +161,7 @@ func writeToRC(shell string, kubectl, crictl bool) error {
return err
}
defer f.Close()
bashEntry := fmt.Sprintf("# >> %[1]s command completion (start)\n. <(%[1]s completion %[2]s%[3]s%[4]s)\n# >> %[1]s command completion (end)", version.Program, shell, isKubectlSet(kubectl), isCrictlSet(crictl))
bashEntry := fmt.Sprintf("# >> %[1]s command completion (start)\n. <(%[1]s completion %[2]s%[3]s%[4]s%[5]s)\n# >> %[1]s command completion (end)", version.Program, shell, isKubectlSet(kubectl), isCrictlSet(crictl), isCtrSet(ctr))
if _, err := f.WriteString(bashEntry); err != nil {
return err
}
Expand Down