github.com/zaquestion/lab@v0.25.1/cmd/milestone_list.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/MakeNowJust/heredoc/v2" 8 "github.com/rsteube/carapace" 9 "github.com/spf13/cobra" 10 gitlab "github.com/xanzy/go-gitlab" 11 "github.com/zaquestion/lab/internal/action" 12 lab "github.com/zaquestion/lab/internal/gitlab" 13 ) 14 15 var milestoneListCmd = &cobra.Command{ 16 Use: "list [remote] [search]", 17 Aliases: []string{"ls", "search"}, 18 Short: "List milestones", 19 Example: heredoc.Doc(` 20 lab milestone list 21 lab milestone list "search term" 22 lab milestone list remote "search term" 23 lab milestone list upstream -s 'closed'`), 24 PersistentPreRun: labPersistentPreRun, 25 Run: func(cmd *cobra.Command, args []string) { 26 rn, milestoneSearch, err := parseArgsRemoteAndProject(args) 27 if err != nil { 28 log.Fatal(err) 29 } 30 31 milestoneState, _ := cmd.Flags().GetString("state") 32 opts := &gitlab.ListMilestonesOptions{ 33 State: &milestoneState, 34 } 35 36 milestoneSearch = strings.ToLower(milestoneSearch) 37 if milestoneSearch != "" { 38 opts.Search = &milestoneSearch 39 } 40 41 milestones, err := lab.MilestoneList(rn, opts) 42 if err != nil { 43 log.Fatal(err) 44 } 45 46 for _, milestone := range milestones { 47 description := "" 48 if milestone.Description != "" { 49 description = " - " + milestone.Description 50 } 51 52 fmt.Printf("%s%s\n", milestone.Title, description) 53 } 54 }, 55 } 56 57 func init() { 58 milestoneListCmd.Flags().StringP("state", "s", "active", "filter milestones by state (active/closed)") 59 milestoneCmd.AddCommand(milestoneListCmd) 60 61 carapace.Gen(milestoneListCmd).FlagCompletion(carapace.ActionMap{ 62 "state": carapace.ActionValues("active", "closed"), 63 }) 64 65 carapace.Gen(milestoneListCmd).PositionalCompletion( 66 action.Remotes(), 67 ) 68 }