From 1787f959ffed4cf0e4fa7ff2808c9aa13b605b29 Mon Sep 17 00:00:00 2001 From: Sebastien Devaux Date: Fri, 4 Jan 2019 15:15:42 +0100 Subject: Adding fields switch to show command to select fields to display. --- commands/show.go | 106 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 37 deletions(-) (limited to 'commands') diff --git a/commands/show.go b/commands/show.go index 935f617c..15b42984 100644 --- a/commands/show.go +++ b/commands/show.go @@ -12,6 +12,10 @@ import ( "github.com/spf13/cobra" ) +var ( + showFieldsQuery []string +) + func runShowBug(cmd *cobra.Command, args []string) error { backend, err := cache.NewRepoCache(repo) if err != nil { @@ -33,49 +37,75 @@ func runShowBug(cmd *cobra.Command, args []string) error { firstComment := snapshot.Comments[0] - // Header - fmt.Printf("[%s] %s %s\n\n", - colors.Yellow(snapshot.Status), - colors.Cyan(snapshot.HumanId()), - snapshot.Title, - ) - - fmt.Printf("%s opened this issue %s\n\n", - colors.Magenta(firstComment.Author.DisplayName()), - firstComment.FormatTimeRel(), - ) - - var labels = make([]string, len(snapshot.Labels)) - for i := range snapshot.Labels { - labels[i] = string(snapshot.Labels[i]) - } - - fmt.Printf("labels: %s\n\n", - strings.Join(labels, ", "), - ) - - // Comments - indent := " " + if showFieldsQuery==nil { + // Header + fmt.Printf("[%s] %s %s\n\n", + colors.Yellow(snapshot.Status), + colors.Cyan(snapshot.HumanId()), + snapshot.Title, + ) - for i, comment := range snapshot.Comments { - var message string - fmt.Printf("%s#%d %s <%s>\n\n", - indent, - i, - comment.Author.DisplayName(), - comment.Author.Email, + fmt.Printf("%s opened this issue %s\n\n", + colors.Magenta(firstComment.Author.DisplayName()), + firstComment.FormatTimeRel(), ) - if comment.Message == "" { - message = colors.GreyBold("No description provided.") - } else { - message = comment.Message + var labels = make([]string, len(snapshot.Labels)) + for i := range snapshot.Labels { + labels[i] = string(snapshot.Labels[i]) } - fmt.Printf("%s%s\n\n\n", - indent, - message, + fmt.Printf("labels: %s\n\n", + strings.Join(labels, ", "), ) + + // Comments + indent := " " + + for i, comment := range snapshot.Comments { + var message string + fmt.Printf("%s#%d %s <%s>\n\n", + indent, + i, + comment.Author.DisplayName(), + comment.Author.Email, + ) + + if comment.Message == "" { + message = colors.GreyBold("No description provided.") + } else { + message = comment.Message + } + + fmt.Printf("%s%s\n\n\n", + indent, + message, + ) + } + } else { + unknownFields:="" + err:=false + for _, field := range showFieldsQuery { + switch field { + case "author": fmt.Printf("%s ",firstComment.Author.DisplayName()) + case "authorEmail": fmt.Printf("%s ",firstComment.Author.Email) + case "createTime": fmt.Printf("%s ",firstComment.FormatTime()) + case "id": fmt.Printf("%s ",snapshot.Id()) + case "labels": + var labels = make([]string, len(snapshot.Labels)) + fmt.Printf("%s ",strings.Join(labels,", ")) + case "shortId": fmt.Printf("%s ",snapshot.HumanId()) + case "status": fmt.Printf("%s ",snapshot.Status) + case "title": fmt.Printf("%s ",snapshot.Title) + default: + unknownFields+=field+" " + err=true + } + } + fmt.Printf("\n") + if err { + return errors.New(fmt.Sprintf("Unsupported fields requested: %s\n",unknownFields)) + } } return nil @@ -90,4 +120,6 @@ var showCmd = &cobra.Command{ func init() { RootCmd.AddCommand(showCmd) + showCmd.Flags().StringSliceVarP(&showFieldsQuery,"fields","f",nil, + "Selects fields to display. Valid values are [author,authorEmail,createTime,id,labels,shortId,status,title]") } -- cgit From 68cbde492da7911f67e9cce561d4c877ae36bdbf Mon Sep 17 00:00:00 2001 From: Sebastien Devaux Date: Fri, 4 Jan 2019 15:38:47 +0100 Subject: Fixed golangci check error about new error from format string --- commands/show.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commands') diff --git a/commands/show.go b/commands/show.go index 15b42984..bc898241 100644 --- a/commands/show.go +++ b/commands/show.go @@ -104,7 +104,7 @@ func runShowBug(cmd *cobra.Command, args []string) error { } fmt.Printf("\n") if err { - return errors.New(fmt.Sprintf("Unsupported fields requested: %s\n",unknownFields)) + return fmt.Errorf("Unsupported fields requested: %s\n",unknownFields) } } -- cgit From 5850116c0dd49bd42413305e2484beb50cbcc914 Mon Sep 17 00:00:00 2001 From: Sebastien Devaux Date: Mon, 7 Jan 2019 18:56:29 +0100 Subject: Simplified show commadn error handling. Exit on first unknown field found in query. --- commands/show.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'commands') diff --git a/commands/show.go b/commands/show.go index bc898241..0b5585f1 100644 --- a/commands/show.go +++ b/commands/show.go @@ -83,8 +83,6 @@ func runShowBug(cmd *cobra.Command, args []string) error { ) } } else { - unknownFields:="" - err:=false for _, field := range showFieldsQuery { switch field { case "author": fmt.Printf("%s ",firstComment.Author.DisplayName()) @@ -98,14 +96,10 @@ func runShowBug(cmd *cobra.Command, args []string) error { case "status": fmt.Printf("%s ",snapshot.Status) case "title": fmt.Printf("%s ",snapshot.Title) default: - unknownFields+=field+" " - err=true + return fmt.Errorf("\nUnsupported field: %s\n",field) } } fmt.Printf("\n") - if err { - return fmt.Errorf("Unsupported fields requested: %s\n",unknownFields) - } } return nil -- cgit From 43d0fe5caec529baa348b44f6e1cba49e87754a7 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 7 Jan 2019 23:08:48 +0100 Subject: commands: show: change for a single valued --field flag --- commands/show.go | 126 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 60 deletions(-) (limited to 'commands') diff --git a/commands/show.go b/commands/show.go index 0b5585f1..56717b3b 100644 --- a/commands/show.go +++ b/commands/show.go @@ -13,7 +13,7 @@ import ( ) var ( - showFieldsQuery []string + showFieldsQuery string ) func runShowBug(cmd *cobra.Command, args []string) error { @@ -37,69 +37,75 @@ func runShowBug(cmd *cobra.Command, args []string) error { firstComment := snapshot.Comments[0] - if showFieldsQuery==nil { - // Header - fmt.Printf("[%s] %s %s\n\n", - colors.Yellow(snapshot.Status), - colors.Cyan(snapshot.HumanId()), - snapshot.Title, - ) + if showFieldsQuery != "" { + switch showFieldsQuery { + case "author": + fmt.Printf("%s\n", firstComment.Author.DisplayName()) + case "authorEmail": + fmt.Printf("%s\n", firstComment.Author.Email) + case "createTime": + fmt.Printf("%s\n", firstComment.FormatTime()) + case "id": + fmt.Printf("%s\n", snapshot.Id()) + case "labels": + var labels = make([]string, len(snapshot.Labels)) + fmt.Printf("%s\n", strings.Join(labels, ", ")) + case "shortId": + fmt.Printf("%s\n", snapshot.HumanId()) + case "status": + fmt.Printf("%s\n", snapshot.Status) + case "title": + fmt.Printf("%s\n", snapshot.Title) + default: + return fmt.Errorf("\nUnsupported field: %s\n", showFieldsQuery) + } - fmt.Printf("%s opened this issue %s\n\n", - colors.Magenta(firstComment.Author.DisplayName()), - firstComment.FormatTimeRel(), - ) + return nil + } - var labels = make([]string, len(snapshot.Labels)) - for i := range snapshot.Labels { - labels[i] = string(snapshot.Labels[i]) - } + // Header + fmt.Printf("[%s] %s %s\n\n", + colors.Yellow(snapshot.Status), + colors.Cyan(snapshot.HumanId()), + snapshot.Title, + ) + + fmt.Printf("%s opened this issue %s\n\n", + colors.Magenta(firstComment.Author.DisplayName()), + firstComment.FormatTimeRel(), + ) + + var labels = make([]string, len(snapshot.Labels)) + for i := range snapshot.Labels { + labels[i] = string(snapshot.Labels[i]) + } - fmt.Printf("labels: %s\n\n", - strings.Join(labels, ", "), + fmt.Printf("labels: %s\n\n", + strings.Join(labels, ", "), + ) + + // Comments + indent := " " + + for i, comment := range snapshot.Comments { + var message string + fmt.Printf("%s#%d %s <%s>\n\n", + indent, + i, + comment.Author.DisplayName(), + comment.Author.Email, ) - // Comments - indent := " " - - for i, comment := range snapshot.Comments { - var message string - fmt.Printf("%s#%d %s <%s>\n\n", - indent, - i, - comment.Author.DisplayName(), - comment.Author.Email, - ) - - if comment.Message == "" { - message = colors.GreyBold("No description provided.") - } else { - message = comment.Message - } - - fmt.Printf("%s%s\n\n\n", - indent, - message, - ) - } - } else { - for _, field := range showFieldsQuery { - switch field { - case "author": fmt.Printf("%s ",firstComment.Author.DisplayName()) - case "authorEmail": fmt.Printf("%s ",firstComment.Author.Email) - case "createTime": fmt.Printf("%s ",firstComment.FormatTime()) - case "id": fmt.Printf("%s ",snapshot.Id()) - case "labels": - var labels = make([]string, len(snapshot.Labels)) - fmt.Printf("%s ",strings.Join(labels,", ")) - case "shortId": fmt.Printf("%s ",snapshot.HumanId()) - case "status": fmt.Printf("%s ",snapshot.Status) - case "title": fmt.Printf("%s ",snapshot.Title) - default: - return fmt.Errorf("\nUnsupported field: %s\n",field) - } + if comment.Message == "" { + message = colors.GreyBold("No description provided.") + } else { + message = comment.Message } - fmt.Printf("\n") + + fmt.Printf("%s%s\n\n\n", + indent, + message, + ) } return nil @@ -114,6 +120,6 @@ var showCmd = &cobra.Command{ func init() { RootCmd.AddCommand(showCmd) - showCmd.Flags().StringSliceVarP(&showFieldsQuery,"fields","f",nil, - "Selects fields to display. Valid values are [author,authorEmail,createTime,id,labels,shortId,status,title]") + showCmd.Flags().StringVarP(&showFieldsQuery, "field", "f", "", + "Select field to display. Valid values are [author,authorEmail,createTime,id,labels,shortId,status,title]") } -- cgit