Skip to content

Commit

Permalink
Add ctr to shell completion
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Savian <vitor.savian@suse.com>
  • Loading branch information
vitorsavian committed Sep 6, 2024
1 parent 93e47f1 commit a2dd825
Showing 1 changed file with 22 additions and 5 deletions.
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

0 comments on commit a2dd825

Please sign in to comment.