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		for _, field := range showFieldsQuery {
 87			switch field {
 88				case "author": fmt.Printf("%s ",firstComment.Author.DisplayName())
 89				case "authorEmail": fmt.Printf("%s ",firstComment.Author.Email)
 90				case "createTime": fmt.Printf("%s ",firstComment.FormatTime())
 91				case "id": fmt.Printf("%s ",snapshot.Id())
 92				case "labels":
 93					var labels = make([]string, len(snapshot.Labels))
 94					fmt.Printf("%s ",strings.Join(labels,", "))
 95				case "shortId": fmt.Printf("%s ",snapshot.HumanId())
 96				case "status": fmt.Printf("%s ",snapshot.Status)
 97				case "title": fmt.Printf("%s ",snapshot.Title)
 98				default:
 99					return fmt.Errorf("\nUnsupported field: %s\n",field)
100			}
101		}
102		fmt.Printf("\n")
103	}
104
105	return nil
106}
107
108var showCmd = &cobra.Command{
109	Use:     "show [<id>]",
110	Short:   "Display the details of a bug",
111	PreRunE: loadRepo,
112	RunE:    runShowBug,
113}
114
115func init() {
116	RootCmd.AddCommand(showCmd)
117	showCmd.Flags().StringSliceVarP(&showFieldsQuery,"fields","f",nil,
118		"Selects fields to display. Valid values are [author,authorEmail,createTime,id,labels,shortId,status,title]")
119}