bug_show.go

  1package bugcmd
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"strings"
  7
  8	"github.com/spf13/cobra"
  9
 10	"github.com/MichaelMure/git-bug/commands/cmdjson"
 11	"github.com/MichaelMure/git-bug/commands/completion"
 12	"github.com/MichaelMure/git-bug/commands/execenv"
 13	"github.com/MichaelMure/git-bug/entities/bug"
 14	"github.com/MichaelMure/git-bug/util/colors"
 15)
 16
 17type bugShowOptions struct {
 18	fields string
 19	format string
 20}
 21
 22func newBugShowCommand() *cobra.Command {
 23	env := execenv.NewEnv()
 24	options := bugShowOptions{}
 25
 26	cmd := &cobra.Command{
 27		Use:     "show [BUG_ID]",
 28		Short:   "Display the details of a bug",
 29		PreRunE: execenv.LoadBackend(env),
 30		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
 31			return runBugShow(env, options, args)
 32		}),
 33		ValidArgsFunction: BugCompletion(env),
 34	}
 35
 36	flags := cmd.Flags()
 37	flags.SortFlags = false
 38
 39	fields := []string{"author", "authorEmail", "createTime", "lastEdit", "humanId",
 40		"id", "labels", "shortId", "status", "title", "actors", "participants"}
 41	flags.StringVarP(&options.fields, "field", "", "",
 42		"Select field to display. Valid values are ["+strings.Join(fields, ",")+"]")
 43	cmd.RegisterFlagCompletionFunc("by", completion.From(fields))
 44	flags.StringVarP(&options.format, "format", "f", "default",
 45		"Select the output formatting style. Valid values are [default,json,org-mode]")
 46
 47	return cmd
 48}
 49
 50func runBugShow(env *execenv.Env, opts bugShowOptions, args []string) error {
 51	b, args, err := ResolveSelected(env.Backend, args)
 52	if err != nil {
 53		return err
 54	}
 55
 56	snap := b.Snapshot()
 57
 58	if len(snap.Comments) == 0 {
 59		return errors.New("invalid bug: no comment")
 60	}
 61
 62	if opts.fields != "" {
 63		switch opts.fields {
 64		case "author":
 65			env.Out.Printf("%s\n", snap.Author.DisplayName())
 66		case "authorEmail":
 67			env.Out.Printf("%s\n", snap.Author.Email())
 68		case "createTime":
 69			env.Out.Printf("%s\n", snap.CreateTime.String())
 70		case "lastEdit":
 71			env.Out.Printf("%s\n", snap.EditTime().String())
 72		case "humanId":
 73			env.Out.Printf("%s\n", snap.Id().Human())
 74		case "id":
 75			env.Out.Printf("%s\n", snap.Id())
 76		case "labels":
 77			for _, l := range snap.Labels {
 78				env.Out.Printf("%s\n", l.String())
 79			}
 80		case "actors":
 81			for _, a := range snap.Actors {
 82				env.Out.Printf("%s\n", a.DisplayName())
 83			}
 84		case "participants":
 85			for _, p := range snap.Participants {
 86				env.Out.Printf("%s\n", p.DisplayName())
 87			}
 88		case "shortId":
 89			env.Out.Printf("%s\n", snap.Id().Human())
 90		case "status":
 91			env.Out.Printf("%s\n", snap.Status)
 92		case "title":
 93			env.Out.Printf("%s\n", snap.Title)
 94		default:
 95			return fmt.Errorf("\nUnsupported field: %s\n", opts.fields)
 96		}
 97
 98		return nil
 99	}
100
101	switch opts.format {
102	case "org-mode":
103		return showOrgModeFormatter(env, snap)
104	case "json":
105		return showJsonFormatter(env, snap)
106	case "default":
107		return showDefaultFormatter(env, snap)
108	default:
109		return fmt.Errorf("unknown format %s", opts.format)
110	}
111}
112
113func showDefaultFormatter(env *execenv.Env, snapshot *bug.Snapshot) error {
114	// Header
115	env.Out.Printf("%s [%s] %s\n\n",
116		colors.Cyan(snapshot.Id().Human()),
117		colors.Yellow(snapshot.Status),
118		snapshot.Title,
119	)
120
121	env.Out.Printf("%s opened this issue %s\n",
122		colors.Magenta(snapshot.Author.DisplayName()),
123		snapshot.CreateTime.String(),
124	)
125
126	env.Out.Printf("This was last edited at %s\n\n",
127		snapshot.EditTime().String(),
128	)
129
130	// Labels
131	var labels = make([]string, len(snapshot.Labels))
132	for i := range snapshot.Labels {
133		labels[i] = string(snapshot.Labels[i])
134	}
135
136	env.Out.Printf("labels: %s\n",
137		strings.Join(labels, ", "),
138	)
139
140	// Actors
141	var actors = make([]string, len(snapshot.Actors))
142	for i := range snapshot.Actors {
143		actors[i] = snapshot.Actors[i].DisplayName()
144	}
145
146	env.Out.Printf("actors: %s\n",
147		strings.Join(actors, ", "),
148	)
149
150	// Participants
151	var participants = make([]string, len(snapshot.Participants))
152	for i := range snapshot.Participants {
153		participants[i] = snapshot.Participants[i].DisplayName()
154	}
155
156	env.Out.Printf("participants: %s\n\n",
157		strings.Join(participants, ", "),
158	)
159
160	// Comments
161	indent := "  "
162
163	for i, comment := range snapshot.Comments {
164		var message string
165		env.Out.Printf("%s%s #%d %s <%s>\n\n",
166			indent,
167			comment.CombinedId().Human(),
168			i,
169			comment.Author.DisplayName(),
170			comment.Author.Email(),
171		)
172
173		if comment.Message == "" {
174			message = colors.BlackBold(colors.WhiteBg("No description provided."))
175		} else {
176			message = comment.Message
177		}
178
179		env.Out.Printf("%s%s\n\n\n",
180			indent,
181			message,
182		)
183	}
184
185	return nil
186}
187
188func showJsonFormatter(env *execenv.Env, snap *bug.Snapshot) error {
189	jsonBug := cmdjson.NewBugSnapshot(snap)
190	return env.Out.PrintJSON(jsonBug)
191}
192
193func showOrgModeFormatter(env *execenv.Env, snapshot *bug.Snapshot) error {
194	// Header
195	env.Out.Printf("%s [%s] %s\n",
196		snapshot.Id().Human(),
197		snapshot.Status,
198		snapshot.Title,
199	)
200
201	env.Out.Printf("* Author: %s\n",
202		snapshot.Author.DisplayName(),
203	)
204
205	env.Out.Printf("* Creation Time: %s\n",
206		snapshot.CreateTime.String(),
207	)
208
209	env.Out.Printf("* Last Edit: %s\n",
210		snapshot.EditTime().String(),
211	)
212
213	// Labels
214	var labels = make([]string, len(snapshot.Labels))
215	for i, label := range snapshot.Labels {
216		labels[i] = string(label)
217	}
218
219	env.Out.Printf("* Labels:\n")
220	if len(labels) > 0 {
221		env.Out.Printf("** %s\n",
222			strings.Join(labels, "\n** "),
223		)
224	}
225
226	// Actors
227	var actors = make([]string, len(snapshot.Actors))
228	for i, actor := range snapshot.Actors {
229		actors[i] = fmt.Sprintf("%s %s",
230			actor.Id().Human(),
231			actor.DisplayName(),
232		)
233	}
234
235	env.Out.Printf("* Actors:\n** %s\n",
236		strings.Join(actors, "\n** "),
237	)
238
239	// Participants
240	var participants = make([]string, len(snapshot.Participants))
241	for i, participant := range snapshot.Participants {
242		participants[i] = fmt.Sprintf("%s %s",
243			participant.Id().Human(),
244			participant.DisplayName(),
245		)
246	}
247
248	env.Out.Printf("* Participants:\n** %s\n",
249		strings.Join(participants, "\n** "),
250	)
251
252	env.Out.Printf("* Comments:\n")
253
254	for i, comment := range snapshot.Comments {
255		var message string
256		env.Out.Printf("** #%d %s\n",
257			i, comment.Author.DisplayName())
258
259		if comment.Message == "" {
260			message = "No description provided."
261		} else {
262			message = strings.ReplaceAll(comment.Message, "\n", "\n: ")
263		}
264
265		env.Out.Printf(": %s\n", message)
266	}
267
268	return nil
269}