1package boardcmd
2
3import (
4 "strings"
5
6 "github.com/spf13/cobra"
7
8 "github.com/git-bug/git-bug/cache"
9 bugcmd "github.com/git-bug/git-bug/commands/bug"
10 "github.com/git-bug/git-bug/commands/completion"
11 "github.com/git-bug/git-bug/commands/execenv"
12 _select "github.com/git-bug/git-bug/commands/select"
13)
14
15// BoardCompletion complete a board id
16func BoardCompletion(env *execenv.Env) completion.ValidArgsFunction {
17 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
18 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
19 return completion.HandleError(err)
20 }
21 defer func() {
22 _ = env.Backend.Close()
23 }()
24
25 return boardWithBackend(env.Backend, toComplete)
26 }
27}
28
29func boardWithBackend(backend *cache.RepoCache, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
30 for _, id := range backend.Boards().AllIds() {
31 if strings.Contains(id.String(), strings.TrimSpace(toComplete)) {
32 excerpt, err := backend.Boards().ResolveExcerpt(id)
33 if err != nil {
34 return completion.HandleError(err)
35 }
36 completions = append(completions, id.Human()+"\t"+excerpt.Title)
37 }
38 }
39
40 return completions, cobra.ShellCompDirectiveNoFileComp
41}
42
43// ColumnCompletion complete a board's column id
44func ColumnCompletion(env *execenv.Env) completion.ValidArgsFunction {
45 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
46 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
47 return completion.HandleError(err)
48 }
49 defer func() {
50 _ = env.Backend.Close()
51 }()
52
53 b, _, err := ResolveSelected(env.Backend, args)
54 switch {
55 case _select.IsErrNoValidId(err):
56 // no completion
57 case err == nil:
58 for _, column := range b.Snapshot().Columns {
59 completions = append(completions, column.CombinedId.Human()+"\t"+column.Name)
60 }
61 default:
62 return completion.HandleError(err)
63 }
64
65 return completions, cobra.ShellCompDirectiveNoFileComp
66 }
67}
68
69func BoardAndBugCompletion(env *execenv.Env) completion.ValidArgsFunction {
70 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
71 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
72 return completion.HandleError(err)
73 }
74 defer func() {
75 _ = env.Backend.Close()
76 }()
77
78 _, _, err := ResolveSelected(env.Backend, args)
79 switch {
80 case _select.IsErrNoValidId(err):
81 return boardWithBackend(env.Backend, toComplete)
82 case err == nil:
83 return bugcmd.BugWithBackend(env.Backend, toComplete)
84 default:
85 return completion.HandleError(err)
86 }
87
88 }
89}