Skip to content
Open
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
19 changes: 19 additions & 0 deletions cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)
}

Expand DownExpand Up@@ -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)
}