1package commands
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/MichaelMure/git-bug/bug"
8 "github.com/spf13/cobra"
9)
10
11func runLsID(cmd *cobra.Command, args []string) error {
12
13 if len(args) < 1 {
14 _, err := ListAllID()
15
16 if err != nil {
17 return err
18 }
19
20 return nil
21 }
22 answer, err := ListID(args[0])
23
24 if err != nil {
25 return err
26 }
27
28 if answer == "" {
29 fmt.Printf("No matching bug Id with prefix %s\n", args[0])
30 } else {
31 fmt.Println(answer)
32 }
33
34 return nil
35}
36
37//ListID lists the local bug id after taking the prefix as input
38func ListID(prefix string) (string, error) {
39
40 IDlist, err := bug.ListLocalIds(repo)
41
42 if err != nil {
43 return "", err
44 }
45
46 for _, id := range IDlist {
47 if strings.HasPrefix(id, prefix) {
48 return id, nil
49 }
50 }
51
52 return "", nil
53
54}
55
56//ListAllID lists all the local bug id
57func ListAllID() (string, error) {
58
59 IDlist, err := bug.ListLocalIds(repo)
60 if err != nil {
61 return "", err
62 }
63
64 for _, id := range IDlist {
65 fmt.Println(id)
66 }
67
68 return "", nil
69}
70
71var listBugIDCmd = &cobra.Command{
72 Use: "ls-id [<prefix>]",
73 Short: "List Bug Id",
74 PreRunE: loadRepo,
75 RunE: runLsID,
76}
77
78func init() {
79 RootCmd.AddCommand(listBugIDCmd)
80}