show.go

  1package commands
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"strings"
  7
  8	"github.com/MichaelMure/git-bug/cache"
  9	"github.com/MichaelMure/git-bug/commands/select"
 10	"github.com/MichaelMure/git-bug/util/colors"
 11	"github.com/MichaelMure/git-bug/util/interrupt"
 12	"github.com/spf13/cobra"
 13)
 14
 15var (
 16	showFieldsQuery	[]string
 17)
 18
 19func runShowBug(cmd *cobra.Command, args []string) error {
 20	backend, err := cache.NewRepoCache(repo)
 21	if err != nil {
 22		return err
 23	}
 24	defer backend.Close()
 25	interrupt.RegisterCleaner(backend.Close)
 26
 27	b, args, err := _select.ResolveBug(backend, args)
 28	if err != nil {
 29		return err
 30	}
 31
 32	snapshot := b.Snapshot()
 33
 34	if len(snapshot.Comments) == 0 {
 35		return errors.New("Invalid bug: no comment")
 36	}
 37
 38	firstComment := snapshot.Comments[0]
 39
 40	if showFieldsQuery==nil {
 41		// Header
 42		fmt.Printf("[%s] %s %s\n\n",
 43			colors.Yellow(snapshot.Status),
 44			colors.Cyan(snapshot.HumanId()),
 45			snapshot.Title,
 46		)
 47
 48		fmt.Printf("%s opened this issue %s\n\n",
 49			colors.Magenta(firstComment.Author.DisplayName()),
 50			firstComment.FormatTimeRel(),
 51		)
 52
 53		var labels = make([]string, len(snapshot.Labels))
 54		for i := range snapshot.Labels {
 55			labels[i] = string(snapshot.Labels[i])
 56		}
 57
 58		fmt.Printf("labels: %s\n\n",
 59			strings.Join(labels, ", "),
 60		)
 61
 62		// Comments
 63		indent := "  "
 64
 65		for i, comment := range snapshot.Comments {
 66			var message string
 67			fmt.Printf("%s#%d %s <%s>\n\n",
 68				indent,
 69				i,
 70				comment.Author.DisplayName(),
 71				comment.Author.Email,
 72			)
 73
 74			if comment.Message == "" {
 75				message = colors.GreyBold("No description provided.")
 76			} else {
 77				message = comment.Message
 78			}
 79
 80			fmt.Printf("%s%s\n\n\n",
 81				indent,
 82				message,
 83			)
 84		}
 85	} else {
 86		unknownFields:=""
 87		err:=false
 88		for _, field := range showFieldsQuery {
 89			switch field {
 90				case "author": fmt.Printf("%s ",firstComment.Author.DisplayName())
 91				case "authorEmail": fmt.Printf("%s ",firstComment.Author.Email)
 92				case "createTime": fmt.Printf("%s ",firstComment.FormatTime())
 93				case "id": fmt.Printf("%s ",snapshot.Id())
 94				case "labels":
 95					var labels = make([]string, len(snapshot.Labels))
 96					fmt.Printf("%s ",strings.Join(labels,", "))
 97				case "shortId": fmt.Printf("%s ",snapshot.HumanId())
 98				case "status": fmt.Printf("%s ",snapshot.Status)
 99				case "title": fmt.Printf("%s ",snapshot.Title)
100				default:
101					unknownFields+=field+" "
102					err=true
103			}
104		}
105		fmt.Printf("\n")
106		if err {
107			return errors.New(fmt.Sprintf("Unsupported fields requested: %s\n",unknownFields))
108		}
109	}
110
111	return nil
112}
113
114var showCmd = &cobra.Command{
115	Use:     "show [<id>]",
116	Short:   "Display the details of a bug",
117	PreRunE: loadRepo,
118	RunE:    runShowBug,
119}
120
121func init() {
122	RootCmd.AddCommand(showCmd)
123	showCmd.Flags().StringSliceVarP(&showFieldsQuery,"fields","f",nil,
124		"Selects fields to display. Valid values are [author,authorEmail,createTime,id,labels,shortId,status,title]")
125}