1package commands
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"strings"
  7
  8	"github.com/MichaelMure/git-bug/cache"
  9	_select "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.Id().Human())
 50		case "id":
 51			fmt.Printf("%s\n", snapshot.Id())
 52		case "labels":
 53			for _, l := range snapshot.Labels {
 54				fmt.Printf("%s\n", l.String())
 55			}
 56		case "actors":
 57			for _, a := range snapshot.Actors {
 58				fmt.Printf("%s\n", a.DisplayName())
 59			}
 60		case "participants":
 61			for _, p := range snapshot.Participants {
 62				fmt.Printf("%s\n", p.DisplayName())
 63			}
 64		case "shortId":
 65			fmt.Printf("%s\n", snapshot.Id().Human())
 66		case "status":
 67			fmt.Printf("%s\n", snapshot.Status)
 68		case "title":
 69			fmt.Printf("%s\n", snapshot.Title)
 70		default:
 71			return fmt.Errorf("\nUnsupported field: %s\n", showFieldsQuery)
 72		}
 73
 74		return nil
 75	}
 76
 77	// Header
 78	fmt.Printf("[%s] %s %s\n\n",
 79		colors.Yellow(snapshot.Status),
 80		colors.Cyan(snapshot.Id().Human()),
 81		snapshot.Title,
 82	)
 83
 84	fmt.Printf("%s opened this issue %s\n\n",
 85		colors.Magenta(firstComment.Author.DisplayName()),
 86		firstComment.FormatTimeRel(),
 87	)
 88
 89	// Labels
 90	var labels = make([]string, len(snapshot.Labels))
 91	for i := range snapshot.Labels {
 92		labels[i] = string(snapshot.Labels[i])
 93	}
 94
 95	fmt.Printf("labels: %s\n",
 96		strings.Join(labels, ", "),
 97	)
 98
 99	// Actors
100	var actors = make([]string, len(snapshot.Actors))
101	for i := range snapshot.Actors {
102		actors[i] = snapshot.Actors[i].DisplayName()
103	}
104
105	fmt.Printf("actors: %s\n",
106		strings.Join(actors, ", "),
107	)
108
109	// Participants
110	var participants = make([]string, len(snapshot.Participants))
111	for i := range snapshot.Participants {
112		participants[i] = snapshot.Participants[i].DisplayName()
113	}
114
115	fmt.Printf("participants: %s\n\n",
116		strings.Join(participants, ", "),
117	)
118
119	// Comments
120	indent := "  "
121
122	for i, comment := range snapshot.Comments {
123		var message string
124		fmt.Printf("%s#%d %s <%s>\n\n",
125			indent,
126			i,
127			comment.Author.DisplayName(),
128			comment.Author.Email(),
129		)
130
131		if comment.Message == "" {
132			message = colors.GreyBold("No description provided.")
133		} else {
134			message = comment.Message
135		}
136
137		fmt.Printf("%s%s\n\n\n",
138			indent,
139			message,
140		)
141	}
142
143	return nil
144}
145
146var showCmd = &cobra.Command{
147	Use:     "show [<id>]",
148	Short:   "Display the details of a bug.",
149	PreRunE: loadRepo,
150	RunE:    runShowBug,
151}
152
153func init() {
154	RootCmd.AddCommand(showCmd)
155	showCmd.Flags().StringVarP(&showFieldsQuery, "field", "f", "",
156		"Select field to display. Valid values are [author,authorEmail,createTime,humanId,id,labels,shortId,status,title,actors,participants]")
157}