github.com/zaquestion/lab@v0.25.1/cmd/todo_list.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "github.com/MakeNowJust/heredoc/v2" 6 "strconv" 7 "strings" 8 9 "github.com/fatih/color" 10 "github.com/spf13/cobra" 11 gitlab "github.com/xanzy/go-gitlab" 12 lab "github.com/zaquestion/lab/internal/gitlab" 13 ) 14 15 var ( 16 todoType string 17 todoNumRet string 18 targetType string 19 todoPretty bool 20 todoAll bool 21 ) 22 23 var todoListCmd = &cobra.Command{ 24 Use: "list", 25 Aliases: []string{"ls"}, 26 Short: "List todos", 27 Example: heredoc.Doc(` 28 lab todo list 29 lab todo list -a 30 lab todo list -n 10 31 lab todo list -p 32 lab todo list -t mr`), 33 PersistentPreRun: labPersistentPreRun, 34 Run: func(cmd *cobra.Command, args []string) { 35 todos, err := todoList(args) 36 if err != nil { 37 log.Fatal(err) 38 } 39 40 pager := newPager(cmd.Flags()) 41 defer pager.Close() 42 43 red := color.New(color.FgRed).SprintFunc() 44 green := color.New(color.FgGreen).SprintFunc() 45 cyan := color.New(color.FgCyan).SprintFunc() 46 47 for _, todo := range todos { 48 if !todoPretty || todo.TargetType == "DesignManagement::Design" || todo.TargetType == "AlertManagement::Alert" { 49 fmt.Printf("%d %s\n", todo.ID, todo.TargetURL) 50 continue 51 } 52 53 var state string 54 if todo.TargetType == "MergeRequest" { 55 state = todo.Target.State 56 if todo.Target.State == "opened" && todo.Target.WorkInProgress { 57 state = "draft" 58 } 59 } else { 60 state = todo.Target.State 61 } 62 63 switch state { 64 case "opened": 65 state = green("open ") 66 case "merged": 67 state = cyan("merged") 68 case "draft": 69 state = green("draft ") 70 default: 71 state = red(state) 72 } 73 74 fmt.Printf("%s %d \"%s\" ", state, todo.ID, todo.Target.Title) 75 76 name := todo.Author.Name 77 if lab.User() == todo.Author.Username { 78 name = "you" 79 } 80 switch todo.ActionName { 81 case "approval_required": 82 fmt.Printf("(approval requested by %s)\n", name) 83 case "assigned": 84 fmt.Printf("(assigned to you by %s)\n", name) 85 case "build_failed": 86 fmt.Printf("(build failed)\n") 87 case "directly_addressed": 88 fmt.Printf("(%s directly addressed you)\n", name) 89 case "marked": 90 fmt.Printf("(Todo Entry added by you)\n") 91 case "mentioned": 92 fmt.Printf("(%s mentioned you)\n", name) 93 case "merge_train_removed": 94 fmt.Printf("(Merge Train was removed)\n") 95 case "review_requested": 96 fmt.Printf("(review requested by %s)\n", name) 97 case "unmergeable": 98 fmt.Printf("(Cannot be merged)\n") 99 default: 100 fmt.Printf("Unknown action %s\n", todo.ActionName) 101 } 102 103 fmt.Printf(" %s\n", todo.TargetURL) 104 } 105 }, 106 } 107 108 func todoList(args []string) ([]*gitlab.Todo, error) { 109 num, err := strconv.Atoi(todoNumRet) 110 if todoAll || (err != nil) { 111 num = -1 112 } 113 114 opts := gitlab.ListTodosOptions{ 115 ListOptions: gitlab.ListOptions{ 116 PerPage: num, 117 }, 118 } 119 120 var lstr = strings.ToLower(todoType) 121 switch { 122 case lstr == "mr": 123 targetType = "MergeRequest" 124 opts.Type = &targetType 125 case lstr == "issue": 126 targetType = "Issue" 127 opts.Type = &targetType 128 } 129 130 return lab.TodoList(opts, num) 131 } 132 133 func init() { 134 todoListCmd.Flags().BoolVarP(&todoPretty, "pretty", "p", false, "provide more infomation in output") 135 todoListCmd.Flags().StringVarP( 136 &todoType, "type", "t", "all", 137 "filter todos by type (all/mr/issue)") 138 todoListCmd.Flags().StringVarP( 139 &todoNumRet, "number", "n", "10", 140 "number of todos to return") 141 todoListCmd.Flags().BoolVarP(&todoAll, "all", "a", false, "list all Todos") 142 143 todoCmd.AddCommand(todoListCmd) 144 }