From ad3470800fb2ea386f8d93cad39e061795cd5665 Mon Sep 17 00:00:00 2001 From: eximus Date: Sat, 17 Feb 2024 20:10:25 -0500 Subject: [PATCH] compact listing commands --- README.md | 12 ++++++++++-- cmd/issue/list.go | 13 +++++++------ cmd/pipeline/list.go | 6 +++++- cmd/pipeline/pipeline.go | 5 +++-- cmd/pr/list.go | 16 +++++----------- util/util.go | 37 +++++++++++++++++++++++-------------- 6 files changed, 53 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 9781bc7..f4d6d87 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,20 @@ jira_token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX(192 characters) # include branch name at beggining of the pull request (useful to link with jira tickets) include_branch_name: true -# define custom status for jira tickets, to more easily filter by the preset options and colorize output +# define custom text icons or text for jira_status, jira_types, pr_status or pipeline_status. The format is as follows: +# identifier: +# values: ["State 1", "State 2"] # this is the string that matches the state being printed +# color: "1;34" # the ANSII sequence for the color used. if I want to remove icon 1;38;5;235;44 +# text: "ACTUAL STATE" # string printed, takes precedence over icon +# icon: "ﲊ" # icon to display +# +# examples bellow: + jira_status: inprogress: values: ["In Progress", "In Progress_T"] icon: "" # ﲊ 羽   - color: "1;34" # if I want to remove icon 1;38;5;235;44 + color: "1;34" todo: values: ["À FAIRE"] icon: "" #   diff --git a/cmd/issue/list.go b/cmd/issue/list.go index 03296f9..7d85d84 100644 --- a/cmd/issue/list.go +++ b/cmd/issue/list.go @@ -25,7 +25,8 @@ var ListCmd = &cobra.Command{ statuses, _ := cmd.Flags().GetStringArray("status") project, _ := cmd.Flags().GetString("project") priority, _ := cmd.Flags().GetBool("priority") - showUsers, _ := cmd.Flags().GetBool("show-users") + showUsers, _ := cmd.Flags().GetBool("users") + showTime, _ := cmd.Flags().GetBool("time") if !cmd.Flags().Changed("project") { branch, err := util.GetCurrentBranch() @@ -54,7 +55,6 @@ var ListCmd = &cobra.Command{ } } - fmt.Println() for issue := range api.GetIssueList(nResults, all, reporter, project, statusConversion, priority) { timeSpent := "-" if issue.Fields.TimeTracking.TimeSpent != " " { @@ -71,9 +71,10 @@ var ListCmd = &cobra.Command{ fmt.Printf(" \033[37mReporter: \033[1m%s \033[37m(%d comments)\033[m\n", issue.Fields.Reporter.DisplayName, issue.Fields.Comment.Total) } } - fmt.Printf(" \033[37mTime spent: \033[1;34m%s\033[m [ %s/%s ]\n", timeSpent, issue.Fields.TimeTracking.RemainingEstimate, issue.Fields.TimeTracking.OriginalEstimate) + if showTime { + fmt.Printf(" \033[37mTime spent: \033[1;34m%s\033[m [ %s/%s ]\n", timeSpent, issue.Fields.TimeTracking.RemainingEstimate, issue.Fields.TimeTracking.OriginalEstimate) + } } - fmt.Println() }, } @@ -90,8 +91,8 @@ func init() { // TODO add way to sort by recent or the ones the user has participated on // display - ListCmd.Flags().BoolP("show-users", "u", false, "show users") - ListCmd.Flags().BoolP("one-line", "o", false, "show one issue per line. \033[31mNot implemented\033[m") + ListCmd.Flags().BoolP("users", "u", false, "show users") + ListCmd.Flags().BoolP("time", "t", false, "show time information") ListCmd.Flags().IntP("number-results", "n", 10, "max number of results retrieve") // sort ListCmd.Flags().BoolP("priority", "P", false, "sort by priority") diff --git a/cmd/pipeline/list.go b/cmd/pipeline/list.go index c921d13..a6178f1 100644 --- a/cmd/pipeline/list.go +++ b/cmd/pipeline/list.go @@ -16,6 +16,7 @@ var ListCmd = &cobra.Command{ Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { pages, _ := cmd.Flags().GetInt("pages") + showAuthor, _ := cmd.Flags().GetBool("author") pipelineChannel := api.GetPipelineList(viper.GetString("repo"), pages, "") for pipeline := range pipelineChannel { if pipeline.State.Result.Name == "" { @@ -32,11 +33,14 @@ var ListCmd = &cobra.Command{ fmt.Printf(" \033[37m%s (%s)\033[m\n", util.TimeDuration(time.Duration(pipeline.DurationInSeconds*1000000000)), util.TimeAgo(pipeline.CreatedOn)) - fmt.Printf(" \033[33m%s\033[m \033[37mTrigger: %s\033[m\n", pipeline.Author.DisplayName, pipeline.Trigger.Name) // \033[37mComments: %d\033[m", + if showAuthor { + fmt.Printf(" \033[33m%s\033[m \033[37mTrigger: %s\033[m\n", pipeline.Author.DisplayName, pipeline.Trigger.Name) // \033[37mComments: %d\033[m", + } } }, } func init() { ListCmd.Flags().Int("pages", 1, "number of pages with results to retrieve") + ListCmd.Flags().BoolP("author", "a", false, "show author information") } diff --git a/cmd/pipeline/pipeline.go b/cmd/pipeline/pipeline.go index 01b0379..fab10ce 100644 --- a/cmd/pipeline/pipeline.go +++ b/cmd/pipeline/pipeline.go @@ -8,8 +8,9 @@ import ( ) var PipelineCmd = &cobra.Command{ - Use: "pipeline", - Short: "Manage pipelines", + Use: "pipeline", + Aliases: []string{"pl"}, + Short: "Manage pipelines", PersistentPreRun: func(cmd *cobra.Command, args []string) { err := viper.BindPFlag("repo", cmd.Flags().Lookup("repo")) cobra.CheckErr(err) diff --git a/cmd/pr/list.go b/cmd/pr/list.go index b6ec48e..ba105f4 100644 --- a/cmd/pr/list.go +++ b/cmd/pr/list.go @@ -30,13 +30,9 @@ var ListCmd = &cobra.Command{ prChannel := api.GetPrList(viper.GetString("repo"), states, author, search, source, destination, pages, status, participants) - // fmt.Printf("\n Pull Requests for \033[1;36m%s\033[m\n\n", viper.GetString("repo")) count := 0 - fmt.Println() for pr := range prChannel { - // if we didn't provide filter don't show the pr status - fmt.Printf("%-19s \033[1;32m#%d\033[m %s \033[1;34m[ %s → %s]\033[m\n", util.FormatPrState(pr.State), pr.ID, pr.Title, pr.Source.Branch.Name, pr.Destination.Branch.Name) - fmt.Printf(" \033[33m%s\033[m \033[37mComments: %d\033[m", pr.Author.Nickname, pr.CommentCount) + fmt.Printf("%s \033[1;32m#%d\033[m %s \033[1;34m[ %s \033[m→\033[1;34m %s ]\033[m \033[33m%s\033[m", util.FormatPrState(pr.State), pr.ID, pr.Title, pr.Source.Branch.Name, pr.Destination.Branch.Name, pr.Author.Nickname) if status { fmt.Printf(" %s", util.FormatPipelineState(pr.Status.State)) } @@ -49,15 +45,13 @@ var ListCmd = &cobra.Command{ outputStr = append(outputStr, fmt.Sprintf("\033[0;37m%s\033[m", participant.User.DisplayName)) } } - fmt.Printf(" ( %s )", strings.Join(outputStr, ", ")) + fmt.Printf("\n \033[37mComments: %d\033[m ( %s )", pr.CommentCount, strings.Join(outputStr, ", ")) } fmt.Println() count++ } if count == 0 { fmt.Printf("\n No pull requests for \033[1;36m%s\033[m\n\n", viper.GetString("repo")) - } else { - fmt.Println() } }, } @@ -65,7 +59,7 @@ var ListCmd = &cobra.Command{ func init() { // filter ListCmd.Flags().StringP("author", "a", "", "filter by author nick name (full nickname is needed due to an API limitation from bitbucket)") - ListCmd.Flags().StringP("search", "S", "", "search pull request with query") + ListCmd.Flags().String("search", "", "search pull request with query") ListCmd.Flags().StringArrayP("state", "s", []string{string(api.OPEN)}, `filter by state. Default: "open". Multiple of these options can be given possible options: "open", "merged", "declined" or "superseded"`) ListCmd.RegisterFlagCompletionFunc("state", stateCompletion) @@ -76,8 +70,8 @@ func init() { ListCmd.Flags().Bool("all", false, "return pull request with all possible states.") ListCmd.Flags().Int("pages", 1, "number of pages with results to retrieve") - ListCmd.Flags().Bool("status", false, "include status of each pull request on the result. (the result will be slower)") - ListCmd.Flags().BoolP("participants", "p", false, "include participant data for each pull request on the result. (the result will be slower)") + ListCmd.Flags().BoolP("status", "S", false, "include status of each pull request on the result. (the result will be slower)") + ListCmd.Flags().BoolP("participants", "p", false, "include participant and comment data for each pull request on the result. (the result will be slower)") } func stateCompletion(comd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/util/util.go b/util/util.go index ac3430b..18b771a 100644 --- a/util/util.go +++ b/util/util.go @@ -15,7 +15,7 @@ import ( "github.com/spf13/viper" ) -type JiraResultConfig struct { +type ResultSwitchConfig struct { Values []string `mapstructure:"values"` Text string `mapstructure:"text"` Icon string `mapstructure:"icon"` @@ -23,18 +23,27 @@ type JiraResultConfig struct { } func FormatPrState(state api.PrState) string { - stateString := "" - switch state { - case "OPEN": - stateString = "\033[1;38;5;235;44m OPEN \033[m" - case "MERGED": - stateString = "\033[1;38;5;235;45m MERGED \033[m" - case "DECLINED": - stateString = "\033[1;38;5;235;41m DECLINED \033[m" - case "SUPERSEDED": - stateString = "\033[1;38;5;235;44m SUPERSEDED \033[m" + str := fmt.Sprintf("\033[1;38;5;235;47m %s \033[m", state) // default + + jiraStatusMap := make(map[string]ResultSwitchConfig) + if err := viper.UnmarshalKey("pr_status", &jiraStatusMap); err != nil { + cobra.CheckErr(err) } - return stateString + + for k, v := range jiraStatusMap { + for _, s := range v.Values { + if s == state.String() { + if v.Text != "" { + str = fmt.Sprintf("\033[%sm %s \033[m", v.Color, v.Text) + } else if v.Icon != "" { + str = fmt.Sprintf("\033[%sm%s\033[m", v.Color, v.Icon) + } else { + str = fmt.Sprintf("\033[%sm %s \033[m", v.Color, strings.ToUpper(k)) + } + } + } + } + return str } func FormatPipelineState(state string) string { @@ -55,7 +64,7 @@ func FormatPipelineState(state string) string { func FormatIssueType(issueType string) string { str := "" - jiraStatusMap := make(map[string]JiraResultConfig) + jiraStatusMap := make(map[string]ResultSwitchConfig) if err := viper.UnmarshalKey("jira_type", &jiraStatusMap); err != nil { cobra.CheckErr(err) } @@ -79,7 +88,7 @@ func FormatIssueType(issueType string) string { func FormatIssueStatus(status string) string { str := fmt.Sprintf("\033[1;38;5;235;47m %s \033[m", status) // default - jiraStatusMap := make(map[string]JiraResultConfig) + jiraStatusMap := make(map[string]ResultSwitchConfig) if err := viper.UnmarshalKey("jira_status", &jiraStatusMap); err != nil { cobra.CheckErr(err) }