Adding fields switch to show command to select fields to display.

Sebastien Devaux created

Change summary

commands/show.go | 106 ++++++++++++++++++++++++++++++++-----------------
1 file changed, 69 insertions(+), 37 deletions(-)

Detailed changes

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]")
 }