diff --git a/cmd/root.go b/cmd/root.go index 91a541d..5067f65 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,6 +31,7 @@ func Execute(currentVersion string) error { info := version.FetchUpdateInfo(rootCmd.Version) defer info.PromptUpdateIfAvailable() ctx := version.WithContext(context.Background(), &info) + patchBashCompletionHelp() return rootCmd.ExecuteContext(ctx) } @@ -194,3 +195,21 @@ func requireAuth(cmd *cobra.Command, args []string) { err = viper.WriteConfig() promptLoginAndExitIf(err != nil) } + +// patchBashCompletionHelp adjusts Cobra's generated bash completion help. +// +// Cobra's default Linux example redirects into /etc/bash_completion.d, which +// fails for non-root shells. Use sudo tee so only the file write is elevated. +func patchBashCompletionHelp() { + rootCmd.InitDefaultCompletionCmd() + + bashCmd, _, err := rootCmd.Find([]string{"completion", "bash"}) + if err != nil || bashCmd == nil { + return + } + + old := "bootdev completion bash > /etc/bash_completion.d/bootdev" + new := "bootdev completion bash | sudo tee /etc/bash_completion.d/bootdev > /dev/null" + + bashCmd.Long = strings.Replace(bashCmd.Long, old, new, 1) +}