From eab6e211fe130d6753a6e40f0e1d070b85a861cf Mon Sep 17 00:00:00 2001 From: Agent DD Date: Fri, 19 Jun 2026 06:49:30 +0000 Subject: [PATCH] fix(cyctl): add explicit completion command to prevent kubeconfig panic The root command's PersistentPreRun loads kubeconfig for every subcommand, causing 'cyctl completion bash' (and other shells) to panic when no kubeconfig is available or when the cluster is unreachable. Adding an explicit completion command with an empty PersistentPreRun overrides the root's hook so that completion generation runs without attempting any Kubernetes connection. Fixes #880 --- cyctl/cmd/completion.go | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cyctl/cmd/completion.go diff --git a/cyctl/cmd/completion.go b/cyctl/cmd/completion.go new file mode 100644 index 000000000..0be25a737 --- /dev/null +++ b/cyctl/cmd/completion.go @@ -0,0 +1,68 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: `To load completions: + +Bash: + $ source <(cyctl completion bash) + + # To load completions for each session, execute once: + # Linux: + $ cyctl completion bash > /etc/bash_completion.d/cyctl + # macOS: + $ cyctl completion bash > /usr/local/etc/bash_completion.d/cyctl + +Zsh: + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ cyctl completion zsh > "${fpath[1]}/_cyctl" + + # You will need to start a new shell for this setup to take effect. + +Fish: + $ cyctl completion fish | source + + # To load completions for each session, execute once: + $ cyctl completion fish > ~/.config/fish/completions/cyctl.fish + +PowerShell: + PS> cyctl completion powershell | Out-String | Invoke-Expression + + # To load completions for every PowerShell session, run: + PS> cyctl completion powershell > cyctl.ps1 + # and source this file from your PowerShell profile. +`, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + // Override root's PersistentPreRun so kubeconfig is not loaded for completions + PersistentPreRun: func(cmd *cobra.Command, args []string) {}, + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + } + }, +} + +func init() { + RootCmd.AddCommand(completionCmd) +}