diff --git a/commands/comment.go b/commands/comment.go index d8995c3ed..3ad0fa363 100644 --- a/commands/comment.go +++ b/commands/comment.go @@ -19,6 +19,7 @@ func newCommentCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runComment(env, args) }, + ValidArgsFunction: completeBugs(env), } cmd.AddCommand(newCommentAddCommand()) diff --git a/commands/helper_completion.go b/commands/helper_completion.go new file mode 100644 index 000000000..4d5883af6 --- /dev/null +++ b/commands/helper_completion.go @@ -0,0 +1,54 @@ +package commands + +import ( + "log" + + "github.com/spf13/cobra" + + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/query" +) + +type validArgsFunction func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) + +func completeBugs(env *Env) validArgsFunction { + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + // Currently, the commands accept at most one bug ID. + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + + if err := loadBackend(env)(cmd, args); err != nil { + log.Println(err) + return nil, cobra.ShellCompDirectiveError + } + defer func() { + err := closeBackend(env)(cmd, args) + if err != nil { + log.Println(err) + } + }() + + q := query.NewQuery() + allIds, err := env.backend.QueryBugs(q) + if err != nil { + log.Println(err) + return nil, cobra.ShellCompDirectiveError + } + + bugExcerpt := make([]*cache.BugExcerpt, len(allIds)) + for i, id := range allIds { + bugExcerpt[i], err = env.backend.ResolveBugExcerpt(id) + if err != nil { + log.Println(err) + return nil, cobra.ShellCompDirectiveError + } + } + + completions := make([]string, len(allIds)) + for i, id := range allIds { + completions[i] = id.Human() + "\t" + bugExcerpt[i].Title + } + return completions, cobra.ShellCompDirectiveDefault + } +} diff --git a/commands/label.go b/commands/label.go index e3ffdb644..391a8b404 100644 --- a/commands/label.go +++ b/commands/label.go @@ -17,6 +17,7 @@ func newLabelCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runLabel(env, args) }, + ValidArgsFunction: completeBugs(env), } cmd.AddCommand(newLabelAddCommand()) diff --git a/commands/rm.go b/commands/rm.go index 3ad4548de..dcb6fffa8 100644 --- a/commands/rm.go +++ b/commands/rm.go @@ -18,6 +18,7 @@ func newRmCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runRm(env, args) }, + ValidArgsFunction: completeBugs(env), } flags := cmd.Flags() diff --git a/commands/select.go b/commands/select.go index 64e38d9bd..0de875b6a 100644 --- a/commands/select.go +++ b/commands/select.go @@ -32,6 +32,7 @@ The complementary command is "git bug deselect" performing the opposite operatio RunE: func(cmd *cobra.Command, args []string) error { return runSelect(env, args) }, + ValidArgsFunction: completeBugs(env), } return cmd diff --git a/commands/show.go b/commands/show.go index 9ebd1926d..6bc0d31b6 100644 --- a/commands/show.go +++ b/commands/show.go @@ -30,6 +30,7 @@ func newShowCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runShow(env, options, args) }, + ValidArgsFunction: completeBugs(env), } flags := cmd.Flags() diff --git a/commands/status.go b/commands/status.go index 4dceb51c3..0e0f54d06 100644 --- a/commands/status.go +++ b/commands/status.go @@ -16,6 +16,7 @@ func newStatusCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runStatus(env, args) }, + ValidArgsFunction: completeBugs(env), } cmd.AddCommand(newStatusCloseCommand()) diff --git a/commands/title.go b/commands/title.go index 7afa07ce7..60e297697 100644 --- a/commands/title.go +++ b/commands/title.go @@ -16,6 +16,7 @@ func newTitleCommand() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { return runTitle(env, args) }, + ValidArgsFunction: completeBugs(env), } cmd.AddCommand(newTitleEditCommand()) diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index 912e87b48..68c8414bb 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -740,6 +740,7 @@ _git-bug_comment() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() } @@ -822,6 +823,7 @@ _git-bug_label() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() } @@ -1002,6 +1004,7 @@ _git-bug_rm() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() } @@ -1022,6 +1025,7 @@ _git-bug_select() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() } @@ -1052,6 +1056,7 @@ _git-bug_show() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() } @@ -1114,6 +1119,7 @@ _git-bug_status() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() } @@ -1181,6 +1187,7 @@ _git-bug_title() must_have_one_flag=() must_have_one_noun=() + has_completion_function=1 noun_aliases=() }