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 != "" {
 41		switch showFieldsQuery {
 42		case "author":
 43			fmt.Printf("%s\n", firstComment.Author.DisplayName())
 44		case "authorEmail":
 45			fmt.Printf("%s\n", firstComment.Author.Email())
 46		case "createTime":
 47			fmt.Printf("%s\n", firstComment.FormatTime())
 48		case "humanId":
 49			fmt.Printf("%s\n", snapshot.HumanId())
 50		case "id":
 51			fmt.Printf("%s\n", snapshot.Id())
 52		case "labels":
 53			var labels = make([]string, len(snapshot.Labels))
 54			for i, l := range snapshot.Labels {
 55				labels[i] = string(l)
 56			}
 57			fmt.Printf("%s\n", strings.Join(labels, "\n"))
 58		case "shortId":
 59			fmt.Printf("%s\n", snapshot.HumanId())
 60		case "status":
 61			fmt.Printf("%s\n", snapshot.Status)
 62		case "title":
 63			fmt.Printf("%s\n", snapshot.Title)
 64		default:
 65			return fmt.Errorf("\nUnsupported field: %s\n", showFieldsQuery)
 66		}
 67
 68		return nil
 69	}
 70
 71	// Header
 72	fmt.Printf("[%s] %s %s\n\n",
 73		colors.Yellow(snapshot.Status),
 74		colors.Cyan(snapshot.HumanId()),
 75		snapshot.Title,
 76	)
 77
 78	fmt.Printf("%s opened this issue %s\n\n",
 79		colors.Magenta(firstComment.Author.DisplayName()),
 80		firstComment.FormatTimeRel(),
 81	)
 82
 83	var labels = make([]string, len(snapshot.Labels))
 84	for i := range snapshot.Labels {
 85		labels[i] = string(snapshot.Labels[i])
 86	}
 87
 88	fmt.Printf("labels: %s\n\n",
 89		strings.Join(labels, ", "),
 90	)
 91
 92	// Comments
 93	indent := "  "
 94
 95	for i, comment := range snapshot.Comments {
 96		var message string
 97		fmt.Printf("%s#%d %s <%s>\n\n",
 98			indent,
 99			i,
100			comment.Author.DisplayName(),
101			comment.Author.Email(),
102		)
103
104		if comment.Message == "" {
105			message = colors.GreyBold("No description provided.")
106		} else {
107			message = comment.Message
108		}
109
110		fmt.Printf("%s%s\n\n\n",
111			indent,
112			message,
113		)
114	}
115
116	return nil
117}
118
119var showCmd = &cobra.Command{
120	Use:     "show [<id>]",
121	Short:   "Display the details of a bug.",
122	PreRunE: loadRepo,
123	RunE:    runShowBug,
124}
125
126func init() {
127	RootCmd.AddCommand(showCmd)
128	showCmd.Flags().StringVarP(&showFieldsQuery, "field", "f", "",
129		"Select field to display. Valid values are [author,authorEmail,createTime,humanId,id,labels,shortId,status,title]")
130}