1package completion
2
3import (
4 "fmt"
5 "sort"
6 "strings"
7
8 "github.com/spf13/cobra"
9
10 "github.com/MichaelMure/git-bug/bridge"
11 "github.com/MichaelMure/git-bug/bridge/core/auth"
12 "github.com/MichaelMure/git-bug/cache"
13 "github.com/MichaelMure/git-bug/commands/bug/select"
14 "github.com/MichaelMure/git-bug/commands/execenv"
15 "github.com/MichaelMure/git-bug/entities/bug"
16)
17
18type ValidArgsFunction func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective)
19
20func handleError(err error) (completions []string, directives cobra.ShellCompDirective) {
21 return nil, cobra.ShellCompDirectiveError
22}
23
24func Bridge(env *execenv.Env) ValidArgsFunction {
25 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
26 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
27 return handleError(err)
28 }
29 defer func() {
30 _ = env.Backend.Close()
31 }()
32
33 bridges, err := bridge.ConfiguredBridges(env.Backend)
34 if err != nil {
35 return handleError(err)
36 }
37
38 completions = make([]string, len(bridges))
39 for i, bridge := range bridges {
40 completions[i] = bridge + "\t" + "Bridge"
41 }
42
43 return completions, cobra.ShellCompDirectiveNoFileComp
44 }
45}
46
47func BridgeAuth(env *execenv.Env) ValidArgsFunction {
48 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
49 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
50 return handleError(err)
51 }
52 defer func() {
53 _ = env.Backend.Close()
54 }()
55
56 creds, err := auth.List(env.Backend)
57 if err != nil {
58 return handleError(err)
59 }
60
61 completions = make([]string, len(creds))
62 for i, cred := range creds {
63 meta := make([]string, 0, len(cred.Metadata()))
64 for k, v := range cred.Metadata() {
65 meta = append(meta, k+":"+v)
66 }
67 sort.Strings(meta)
68 metaFmt := strings.Join(meta, ",")
69
70 completions[i] = cred.ID().Human() + "\t" + cred.Target() + " " + string(cred.Kind()) + " " + metaFmt
71 }
72
73 return completions, cobra.ShellCompDirectiveNoFileComp
74 }
75}
76
77func Bug(env *execenv.Env) ValidArgsFunction {
78 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
79 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
80 return handleError(err)
81 }
82 defer func() {
83 _ = env.Backend.Close()
84 }()
85
86 return bugWithBackend(env.Backend, toComplete)
87 }
88}
89
90func bugWithBackend(backend *cache.RepoCache, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
91 allIds := backend.AllBugsIds()
92 bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
93 for i, id := range allIds {
94 var err error
95 bugExcerpt[i], err = backend.ResolveBugExcerpt(id)
96 if err != nil {
97 return handleError(err)
98 }
99 }
100
101 for i, id := range allIds {
102 if strings.Contains(id.String(), strings.TrimSpace(toComplete)) {
103 completions = append(completions, id.Human()+"\t"+bugExcerpt[i].Title)
104 }
105 }
106
107 return completions, cobra.ShellCompDirectiveNoFileComp
108}
109
110func BugAndLabels(env *execenv.Env, addOrRemove bool) ValidArgsFunction {
111 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
112 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
113 return handleError(err)
114 }
115 defer func() {
116 _ = env.Backend.Close()
117 }()
118
119 b, args, err := _select.ResolveBug(env.Backend, args)
120 if err == _select.ErrNoValidId {
121 // we need a bug first to complete labels
122 return bugWithBackend(env.Backend, toComplete)
123 }
124 if err != nil {
125 return handleError(err)
126 }
127
128 snap := b.Snapshot()
129
130 seenLabels := map[bug.Label]bool{}
131 for _, label := range args {
132 seenLabels[bug.Label(label)] = addOrRemove
133 }
134
135 var labels []bug.Label
136 if addOrRemove {
137 for _, label := range snap.Labels {
138 seenLabels[label] = true
139 }
140
141 allLabels := env.Backend.ValidLabels()
142 labels = make([]bug.Label, 0, len(allLabels))
143 for _, label := range allLabels {
144 if !seenLabels[label] {
145 labels = append(labels, label)
146 }
147 }
148 } else {
149 labels = make([]bug.Label, 0, len(snap.Labels))
150 for _, label := range snap.Labels {
151 if seenLabels[label] {
152 labels = append(labels, label)
153 }
154 }
155 }
156
157 completions = make([]string, len(labels))
158 for i, label := range labels {
159 completions[i] = string(label) + "\t" + "Label"
160 }
161
162 return completions, cobra.ShellCompDirectiveNoFileComp
163 }
164}
165
166func From(choices []string) ValidArgsFunction {
167 return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
168 return choices, cobra.ShellCompDirectiveNoFileComp
169 }
170}
171
172func GitRemote(env *execenv.Env) ValidArgsFunction {
173 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
174 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
175 return handleError(err)
176 }
177 defer func() {
178 _ = env.Backend.Close()
179 }()
180
181 remoteMap, err := env.Backend.GetRemotes()
182 if err != nil {
183 return handleError(err)
184 }
185 completions = make([]string, 0, len(remoteMap))
186 for remote, url := range remoteMap {
187 completions = append(completions, remote+"\t"+"Remote: "+url)
188 }
189 sort.Strings(completions)
190 return completions, cobra.ShellCompDirectiveNoFileComp
191 }
192}
193
194func Label(env *execenv.Env) ValidArgsFunction {
195 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
196 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
197 return handleError(err)
198 }
199 defer func() {
200 _ = env.Backend.Close()
201 }()
202
203 labels := env.Backend.ValidLabels()
204 completions = make([]string, len(labels))
205 for i, label := range labels {
206 if strings.Contains(label.String(), " ") {
207 completions[i] = fmt.Sprintf("\"%s\"\tLabel", label.String())
208 } else {
209 completions[i] = fmt.Sprintf("%s\tLabel", label.String())
210 }
211 }
212 return completions, cobra.ShellCompDirectiveNoFileComp
213 }
214}
215
216func Ls(env *execenv.Env) ValidArgsFunction {
217 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
218 if strings.HasPrefix(toComplete, "status:") {
219 completions = append(completions, "status:open\tOpen bugs")
220 completions = append(completions, "status:closed\tClosed bugs")
221 return completions, cobra.ShellCompDirectiveDefault
222 }
223
224 byPerson := []string{"author:", "participant:", "actor:"}
225 byLabel := []string{"label:", "no:"}
226 needBackend := false
227 for _, key := range append(byPerson, byLabel...) {
228 if strings.HasPrefix(toComplete, key) {
229 needBackend = true
230 }
231 }
232
233 if needBackend {
234 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
235 return handleError(err)
236 }
237 defer func() {
238 _ = env.Backend.Close()
239 }()
240 }
241
242 for _, key := range byPerson {
243 if !strings.HasPrefix(toComplete, key) {
244 continue
245 }
246 ids := env.Backend.AllIdentityIds()
247 completions = make([]string, len(ids))
248 for i, id := range ids {
249 user, err := env.Backend.ResolveIdentityExcerpt(id)
250 if err != nil {
251 return handleError(err)
252 }
253 var handle string
254 if user.Login != "" {
255 handle = user.Login
256 } else {
257 // "author:John Doe" does not work yet, so use the first name.
258 handle = strings.Split(user.Name, " ")[0]
259 }
260 completions[i] = key + handle + "\t" + user.DisplayName()
261 }
262 return completions, cobra.ShellCompDirectiveNoFileComp
263 }
264
265 for _, key := range byLabel {
266 if !strings.HasPrefix(toComplete, key) {
267 continue
268 }
269 labels := env.Backend.ValidLabels()
270 completions = make([]string, len(labels))
271 for i, label := range labels {
272 if strings.Contains(label.String(), " ") {
273 completions[i] = key + "\"" + string(label) + "\""
274 } else {
275 completions[i] = key + string(label)
276 }
277 }
278 return completions, cobra.ShellCompDirectiveNoFileComp
279 }
280
281 completions = []string{
282 "actor:\tFilter by actor",
283 "author:\tFilter by author",
284 "label:\tFilter by label",
285 "no:\tExclude bugs by label",
286 "participant:\tFilter by participant",
287 "status:\tFilter by open/close status",
288 "title:\tFilter by title",
289 }
290 return completions, cobra.ShellCompDirectiveNoSpace
291 }
292}
293
294func User(env *execenv.Env) ValidArgsFunction {
295 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
296 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
297 return handleError(err)
298 }
299 defer func() {
300 _ = env.Backend.Close()
301 }()
302
303 ids := env.Backend.AllIdentityIds()
304 completions = make([]string, len(ids))
305 for i, id := range ids {
306 user, err := env.Backend.ResolveIdentityExcerpt(id)
307 if err != nil {
308 return handleError(err)
309 }
310 completions[i] = user.Id.Human() + "\t" + user.DisplayName()
311 }
312 return completions, cobra.ShellCompDirectiveNoFileComp
313 }
314}
315
316func UserForQuery(env *execenv.Env) ValidArgsFunction {
317 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
318 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
319 return handleError(err)
320 }
321 defer func() {
322 _ = env.Backend.Close()
323 }()
324
325 ids := env.Backend.AllIdentityIds()
326 completions = make([]string, len(ids))
327 for i, id := range ids {
328 user, err := env.Backend.ResolveIdentityExcerpt(id)
329 if err != nil {
330 return handleError(err)
331 }
332 var handle string
333 if user.Login != "" {
334 handle = user.Login
335 } else {
336 // "author:John Doe" does not work yet, so use the first name.
337 handle = strings.Split(user.Name, " ")[0]
338 }
339 completions[i] = handle + "\t" + user.DisplayName()
340 }
341 return completions, cobra.ShellCompDirectiveNoFileComp
342 }
343}