From dddb1c446f20b0d775f4472d826b754afae0e56f Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Wed, 23 Sep 2026 12:10:54 +0000 Subject: [PATCH] Add completion bash to alias roam to git completion Mimic Cobra by printing the bash script to stdout, but just emit complete -F _git roam since roam wraps git. Reviewed-by: Nathan Nutter Reviewed-at: c6168cf67c2720ce643812a0089f13b1a4387f0f --- README.md | 2 +- completion.go | 13 ++++++++++++- completion_test.go | 12 +++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6efb51d..fb9e7ac 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ My one slight annoyance is the manual set up required for a new host/account whi ### Bash ```bash -complete -F _git roam +eval "$(roam completion bash)" ``` Maybe somehow also use, diff --git a/completion.go b/completion.go index acd72a3..cfd9d7c 100644 --- a/completion.go +++ b/completion.go @@ -46,6 +46,17 @@ func (a *app) completionCommand() *cobra.Command { } var force bool out := a.defaultZshOut() + bash := &cobra.Command{ + Use: "bash", + Short: "Generate the autocompletion script for bash", + Args: cobra.NoArgs, + SilenceUsage: true, + SilenceErrors: true, + RunE: func(cmd *cobra.Command, _ []string) error { + _, err := fmt.Fprintln(cmd.OutOrStdout(), "complete -F _git roam") + return err + }, + } zsh := &cobra.Command{ Use: "zsh", Short: "Generate the autocompletion script for zsh", @@ -58,7 +69,7 @@ func (a *app) completionCommand() *cobra.Command { } zsh.Flags().BoolVarP(&force, "force", "f", false, "Overwrite existing generated files") zsh.Flags().StringVarP(&out, "out", "o", out, "Output directory for generated files (~/.local/share/zsh/site-functions)") - cmd.AddCommand(zsh) + cmd.AddCommand(bash, zsh) return cmd } diff --git a/completion_test.go b/completion_test.go index dcfc4a6..f03d5d8 100644 --- a/completion_test.go +++ b/completion_test.go @@ -9,6 +9,14 @@ import ( "github.com/stretchr/testify/require" ) +func TestCompletionBash(t *testing.T) { + app, _ := testApp(t) + var stdout bytes.Buffer + app.stdout = &stdout + require.NoError(t, app.run(t.Context(), []string{"completion", "bash"})) + require.Equal(t, "complete -F _git roam\n", stdout.String()) +} + func TestCompletionZsh(t *testing.T) { app, _ := testApp(t) out := filepath.Join(app.home, "completions") @@ -67,7 +75,9 @@ func TestCompletionHelp(t *testing.T) { var stdout bytes.Buffer app.stdout = &stdout require.NoError(t, app.run(t.Context(), []string{"completion", "-h"})) - require.Contains(t, stdout.String(), "zsh") + help := stdout.String() + require.Contains(t, help, "bash") + require.Contains(t, help, "zsh") } func TestCompletionZshTildeOut(t *testing.T) {