2018-07-17 21:51:09 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-06-21 08:51:48 +03:00
|
|
|
"encoding/json"
|
2018-07-17 21:51:09 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-06-24 15:52:48 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-06-21 08:51:48 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-04-05 17:42:45 +03:00
|
|
|
_select "github.com/MichaelMure/git-bug/commands/select"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/colors"
|
2018-07-17 21:51:09 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type showOptions struct {
|
2020-07-28 21:24:24 +03:00
|
|
|
fields string
|
2020-06-28 19:26:29 +03:00
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newShowCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := showOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2020-07-28 21:24:24 +03:00
|
|
|
Use: "show [ID]",
|
2020-06-28 20:09:32 +03:00
|
|
|
Short: "Display the details of a bug.",
|
|
|
|
PreRunE: loadBackend(env),
|
|
|
|
PostRunE: closeBackend(env),
|
2020-06-28 19:26:29 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runShow(env, options, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SortFlags = false
|
2019-01-04 17:15:42 +03:00
|
|
|
|
2020-07-28 21:24:24 +03:00
|
|
|
flags.StringVarP(&options.fields, "field", "", "",
|
2020-06-28 19:26:29 +03:00
|
|
|
"Select field to display. Valid values are [author,authorEmail,createTime,lastEdit,humanId,id,labels,shortId,status,title,actors,participants]")
|
|
|
|
flags.StringVarP(&options.format, "format", "f", "default",
|
|
|
|
"Select the output formatting style. Valid values are [default,json,org-mode]")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runShow(env *Env, opts showOptions, args []string) error {
|
2020-06-28 20:09:32 +03:00
|
|
|
b, args, err := _select.ResolveBug(env.backend, args)
|
2018-07-17 21:51:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
snap := b.Snapshot()
|
2018-07-17 21:51:09 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
if len(snap.Comments) == 0 {
|
2019-04-03 22:05:58 +03:00
|
|
|
return errors.New("invalid bug: no comment")
|
2018-07-17 21:51:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 21:24:24 +03:00
|
|
|
if opts.fields != "" {
|
|
|
|
switch opts.fields {
|
2019-01-08 01:08:48 +03:00
|
|
|
case "author":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Author.DisplayName())
|
2019-01-08 01:08:48 +03:00
|
|
|
case "authorEmail":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Author.Email())
|
2019-01-08 01:08:48 +03:00
|
|
|
case "createTime":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.CreateTime.String())
|
2020-06-21 08:51:48 +03:00
|
|
|
case "lastEdit":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.EditTime().String())
|
2019-04-03 22:33:04 +03:00
|
|
|
case "humanId":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Id().Human())
|
2019-01-08 01:08:48 +03:00
|
|
|
case "id":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Id())
|
2019-01-08 01:08:48 +03:00
|
|
|
case "labels":
|
2020-06-28 19:26:29 +03:00
|
|
|
for _, l := range snap.Labels {
|
|
|
|
env.out.Printf("%s\n", l.String())
|
2019-04-05 17:42:45 +03:00
|
|
|
}
|
|
|
|
case "actors":
|
2020-06-28 19:26:29 +03:00
|
|
|
for _, a := range snap.Actors {
|
|
|
|
env.out.Printf("%s\n", a.DisplayName())
|
2019-04-05 17:42:45 +03:00
|
|
|
}
|
|
|
|
case "participants":
|
2020-06-28 19:26:29 +03:00
|
|
|
for _, p := range snap.Participants {
|
|
|
|
env.out.Printf("%s\n", p.DisplayName())
|
2019-04-03 22:05:58 +03:00
|
|
|
}
|
2019-01-08 01:08:48 +03:00
|
|
|
case "shortId":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Id().Human())
|
2019-01-08 01:08:48 +03:00
|
|
|
case "status":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Status)
|
2019-01-08 01:08:48 +03:00
|
|
|
case "title":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", snap.Title)
|
2019-01-08 01:08:48 +03:00
|
|
|
default:
|
2020-07-28 21:24:24 +03:00
|
|
|
return fmt.Errorf("\nUnsupported field: %s\n", opts.fields)
|
2019-01-08 01:08:48 +03:00
|
|
|
}
|
2018-07-17 21:51:09 +03:00
|
|
|
|
2019-01-08 01:08:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-17 21:51:09 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
switch opts.format {
|
2020-06-23 12:51:42 +03:00
|
|
|
case "org-mode":
|
2020-06-28 19:26:29 +03:00
|
|
|
return showOrgModeFormatter(env, snap)
|
2020-06-21 08:51:48 +03:00
|
|
|
case "json":
|
2020-06-28 19:26:29 +03:00
|
|
|
return showJsonFormatter(env, snap)
|
2020-06-21 08:51:48 +03:00
|
|
|
case "default":
|
2020-06-28 19:26:29 +03:00
|
|
|
return showDefaultFormatter(env, snap)
|
2020-06-21 08:51:48 +03:00
|
|
|
default:
|
2020-06-28 19:26:29 +03:00
|
|
|
return fmt.Errorf("unknown format %s", opts.format)
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func showDefaultFormatter(env *Env, snapshot *bug.Snapshot) error {
|
2019-01-08 01:08:48 +03:00
|
|
|
// Header
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s [%s] %s\n\n",
|
2019-08-12 17:12:14 +03:00
|
|
|
colors.Cyan(snapshot.Id().Human()),
|
2020-06-23 12:51:42 +03:00
|
|
|
colors.Yellow(snapshot.Status),
|
2019-01-08 01:08:48 +03:00
|
|
|
snapshot.Title,
|
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s opened this issue %s\n",
|
2020-06-21 08:51:48 +03:00
|
|
|
colors.Magenta(snapshot.Author.DisplayName()),
|
2020-06-26 00:18:17 +03:00
|
|
|
snapshot.CreateTime.String(),
|
2020-06-21 08:51:48 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("This was last edited at %s\n\n",
|
2020-06-26 00:18:17 +03:00
|
|
|
snapshot.EditTime().String(),
|
2019-01-08 01:08:48 +03:00
|
|
|
)
|
|
|
|
|
2019-04-05 17:42:45 +03:00
|
|
|
// Labels
|
2019-01-08 01:08:48 +03:00
|
|
|
var labels = make([]string, len(snapshot.Labels))
|
|
|
|
for i := range snapshot.Labels {
|
|
|
|
labels[i] = string(snapshot.Labels[i])
|
|
|
|
}
|
2018-12-22 06:36:32 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("labels: %s\n",
|
2019-01-08 01:08:48 +03:00
|
|
|
strings.Join(labels, ", "),
|
|
|
|
)
|
|
|
|
|
2019-04-05 17:42:45 +03:00
|
|
|
// Actors
|
|
|
|
var actors = make([]string, len(snapshot.Actors))
|
|
|
|
for i := range snapshot.Actors {
|
|
|
|
actors[i] = snapshot.Actors[i].DisplayName()
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("actors: %s\n",
|
2019-04-05 17:42:45 +03:00
|
|
|
strings.Join(actors, ", "),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Participants
|
|
|
|
var participants = make([]string, len(snapshot.Participants))
|
|
|
|
for i := range snapshot.Participants {
|
|
|
|
participants[i] = snapshot.Participants[i].DisplayName()
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("participants: %s\n\n",
|
2019-04-05 17:42:45 +03:00
|
|
|
strings.Join(participants, ", "),
|
|
|
|
)
|
|
|
|
|
2019-01-08 01:08:48 +03:00
|
|
|
// Comments
|
|
|
|
indent := " "
|
|
|
|
|
|
|
|
for i, comment := range snapshot.Comments {
|
|
|
|
var message string
|
2020-07-09 09:59:47 +03:00
|
|
|
env.out.Printf("%s%s #%d %s <%s>\n\n",
|
2019-01-08 01:08:48 +03:00
|
|
|
indent,
|
2020-07-09 09:59:47 +03:00
|
|
|
comment.Id().Human(),
|
2019-01-08 01:08:48 +03:00
|
|
|
i,
|
|
|
|
comment.Author.DisplayName(),
|
2019-01-19 18:01:06 +03:00
|
|
|
comment.Author.Email(),
|
2018-07-17 21:51:09 +03:00
|
|
|
)
|
2019-01-04 17:15:42 +03:00
|
|
|
|
2019-01-08 01:08:48 +03:00
|
|
|
if comment.Message == "" {
|
2020-09-21 13:11:39 +03:00
|
|
|
message = colors.BlackBold(colors.WhiteBg("No description provided."))
|
2019-01-08 01:08:48 +03:00
|
|
|
} else {
|
|
|
|
message = comment.Message
|
2019-01-04 17:15:42 +03:00
|
|
|
}
|
2019-01-08 01:08:48 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s%s\n\n\n",
|
2019-01-08 01:08:48 +03:00
|
|
|
indent,
|
|
|
|
message,
|
|
|
|
)
|
2018-07-17 21:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-21 08:51:48 +03:00
|
|
|
type JSONBugSnapshot struct {
|
2020-06-26 00:18:17 +03:00
|
|
|
Id string `json:"id"`
|
|
|
|
HumanId string `json:"human_id"`
|
|
|
|
CreateTime JSONTime `json:"create_time"`
|
|
|
|
EditTime JSONTime `json:"edit_time"`
|
2020-06-21 08:51:48 +03:00
|
|
|
Status string `json:"status"`
|
|
|
|
Labels []bug.Label `json:"labels"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Author JSONIdentity `json:"author"`
|
|
|
|
Actors []JSONIdentity `json:"actors"`
|
|
|
|
Participants []JSONIdentity `json:"participants"`
|
2020-06-26 00:18:17 +03:00
|
|
|
Comments []JSONComment `json:"comments"`
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type JSONComment struct {
|
2020-06-26 00:18:17 +03:00
|
|
|
Id string `json:"id"`
|
|
|
|
HumanId string `json:"human_id"`
|
2020-06-24 15:52:48 +03:00
|
|
|
Author JSONIdentity `json:"author"`
|
|
|
|
Message string `json:"message"`
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 00:18:17 +03:00
|
|
|
func NewJSONComment(comment bug.Comment) JSONComment {
|
|
|
|
return JSONComment{
|
|
|
|
Id: comment.Id().String(),
|
|
|
|
HumanId: comment.Id().Human(),
|
|
|
|
Author: NewJSONIdentity(comment.Author),
|
|
|
|
Message: comment.Message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func showJsonFormatter(env *Env, snapshot *bug.Snapshot) error {
|
2020-06-21 08:51:48 +03:00
|
|
|
jsonBug := JSONBugSnapshot{
|
2020-06-26 00:18:17 +03:00
|
|
|
Id: snapshot.Id().String(),
|
|
|
|
HumanId: snapshot.Id().Human(),
|
|
|
|
CreateTime: NewJSONTime(snapshot.CreateTime, 0),
|
|
|
|
EditTime: NewJSONTime(snapshot.EditTime(), 0),
|
|
|
|
Status: snapshot.Status.String(),
|
|
|
|
Labels: snapshot.Labels,
|
|
|
|
Title: snapshot.Title,
|
|
|
|
Author: NewJSONIdentity(snapshot.Author),
|
2020-06-24 05:22:32 +03:00
|
|
|
}
|
2020-06-21 08:51:48 +03:00
|
|
|
|
2020-06-24 15:52:48 +03:00
|
|
|
jsonBug.Actors = make([]JSONIdentity, len(snapshot.Actors))
|
|
|
|
for i, element := range snapshot.Actors {
|
|
|
|
jsonBug.Actors[i] = NewJSONIdentity(element)
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:52:48 +03:00
|
|
|
jsonBug.Participants = make([]JSONIdentity, len(snapshot.Participants))
|
|
|
|
for i, element := range snapshot.Participants {
|
|
|
|
jsonBug.Participants[i] = NewJSONIdentity(element)
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 00:18:17 +03:00
|
|
|
jsonBug.Comments = make([]JSONComment, len(snapshot.Comments))
|
2020-06-21 08:51:48 +03:00
|
|
|
for i, comment := range snapshot.Comments {
|
2020-06-26 00:18:17 +03:00
|
|
|
jsonBug.Comments[i] = NewJSONComment(comment)
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonObject, _ := json.MarshalIndent(jsonBug, "", " ")
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", jsonObject)
|
2020-06-21 08:51:48 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func showOrgModeFormatter(env *Env, snapshot *bug.Snapshot) error {
|
2020-06-23 12:51:42 +03:00
|
|
|
// Header
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s [%s] %s\n",
|
2020-06-23 12:51:42 +03:00
|
|
|
snapshot.Id().Human(),
|
|
|
|
snapshot.Status,
|
|
|
|
snapshot.Title,
|
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Author: %s\n",
|
2020-06-23 12:51:42 +03:00
|
|
|
snapshot.Author.DisplayName(),
|
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Creation Time: %s\n",
|
2020-06-26 00:18:17 +03:00
|
|
|
snapshot.CreateTime.String(),
|
2020-06-23 12:51:42 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Last Edit: %s\n",
|
2020-06-26 00:18:17 +03:00
|
|
|
snapshot.EditTime().String(),
|
2020-06-23 12:51:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Labels
|
|
|
|
var labels = make([]string, len(snapshot.Labels))
|
|
|
|
for i, label := range snapshot.Labels {
|
|
|
|
labels[i] = string(label)
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Labels:\n")
|
2020-06-23 12:51:42 +03:00
|
|
|
if len(labels) > 0 {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("** %s\n",
|
2020-06-24 09:34:36 +03:00
|
|
|
strings.Join(labels, "\n** "),
|
|
|
|
)
|
2020-06-23 12:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actors
|
|
|
|
var actors = make([]string, len(snapshot.Actors))
|
|
|
|
for i, actor := range snapshot.Actors {
|
|
|
|
actors[i] = fmt.Sprintf("%s %s",
|
|
|
|
actor.Id().Human(),
|
|
|
|
actor.DisplayName(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Actors:\n** %s\n",
|
2020-06-24 09:34:36 +03:00
|
|
|
strings.Join(actors, "\n** "),
|
2020-06-23 12:51:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Participants
|
|
|
|
var participants = make([]string, len(snapshot.Participants))
|
|
|
|
for i, participant := range snapshot.Participants {
|
2020-06-24 09:34:36 +03:00
|
|
|
participants[i] = fmt.Sprintf("%s %s",
|
2020-06-23 12:51:42 +03:00
|
|
|
participant.Id().Human(),
|
|
|
|
participant.DisplayName(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Participants:\n** %s\n",
|
2020-06-24 09:34:36 +03:00
|
|
|
strings.Join(participants, "\n** "),
|
2020-06-23 12:51:42 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("* Comments:\n")
|
2020-06-23 12:51:42 +03:00
|
|
|
|
|
|
|
for i, comment := range snapshot.Comments {
|
|
|
|
var message string
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("** #%d %s\n",
|
|
|
|
i, comment.Author.DisplayName())
|
2020-06-23 12:51:42 +03:00
|
|
|
|
|
|
|
if comment.Message == "" {
|
|
|
|
message = "No description provided."
|
|
|
|
} else {
|
2020-06-24 05:22:32 +03:00
|
|
|
message = strings.ReplaceAll(comment.Message, "\n", "\n: ")
|
2020-06-23 12:51:42 +03:00
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf(": %s\n", message)
|
2020-06-23 12:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|