completion.go

 1package boardcmd
 2
 3import (
 4	"strings"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/git-bug/git-bug/commands/completion"
 9	"github.com/git-bug/git-bug/commands/execenv"
10)
11
12// BoardCompletion complete a board id
13func BoardCompletion(env *execenv.Env) completion.ValidArgsFunction {
14	return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
15		if err := execenv.LoadBackend(env)(cmd, args); err != nil {
16			return completion.HandleError(err)
17		}
18		defer func() {
19			_ = env.Backend.Close()
20		}()
21
22		for _, id := range env.Backend.Boards().AllIds() {
23			if strings.Contains(id.String(), strings.TrimSpace(toComplete)) {
24				excerpt, err := env.Backend.Boards().ResolveExcerpt(id)
25				if err != nil {
26					return completion.HandleError(err)
27				}
28				completions = append(completions, id.Human()+"\t"+excerpt.Title)
29			}
30		}
31
32		return completions, cobra.ShellCompDirectiveNoFileComp
33	}
34}
35
36func ColumnCompletion(env *execenv.Env) completion.ValidArgsFunction {
37	return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
38		if err := execenv.LoadBackend(env)(cmd, args); err != nil {
39			return completion.HandleError(err)
40		}
41		defer func() {
42			_ = env.Backend.Close()
43		}()
44
45		b, _, err := ResolveSelected(env.Backend, args)
46		if err != nil {
47			return completion.HandleError(err)
48		}
49
50		for _, column := range b.Snapshot().Columns {
51			completions = append(completions, column.Id.Human()+"\t"+column.Name)
52		}
53
54		return completions, cobra.ShellCompDirectiveNoFileComp
55	}
56}