git-bug/commands/show.go

323 lines
7.6 KiB
Go
Raw Normal View History

2018-07-17 21:51:09 +03:00
package commands
import (
"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"
"github.com/MichaelMure/git-bug/bug"
_select "github.com/MichaelMure/git-bug/commands/select"
"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 {
fields string
2020-06-28 19:26:29 +03:00
format string
}
func newShowCommand() *cobra.Command {
env := newEnv()
options := showOptions{}
cmd := &cobra.Command{
Use: "show [ID]",
Short: "Display the details of a bug.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
2020-06-28 19:26:29 +03:00
return runShow(env, options, args)
}),
2020-06-28 19:26:29 +03:00
}
flags := cmd.Flags()
flags.SortFlags = false
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 {
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 {
return errors.New("invalid bug: no comment")
2018-07-17 21:51:09 +03:00
}
if opts.fields != "" {
switch opts.fields {
case "author":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.Author.DisplayName())
case "authorEmail":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.Author.Email())
case "createTime":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.CreateTime.String())
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())
case "id":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.Id())
case "labels":
2020-06-28 19:26:29 +03:00
for _, l := range snap.Labels {
env.out.Printf("%s\n", l.String())
}
case "actors":
2020-06-28 19:26:29 +03:00
for _, a := range snap.Actors {
env.out.Printf("%s\n", a.DisplayName())
}
case "participants":
2020-06-28 19:26:29 +03:00
for _, p := range snap.Participants {
env.out.Printf("%s\n", p.DisplayName())
}
case "shortId":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.Id().Human())
case "status":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.Status)
case "title":
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", snap.Title)
default:
return fmt.Errorf("\nUnsupported field: %s\n", opts.fields)
}
2018-07-17 21:51:09 +03:00
return nil
}
2018-07-17 21:51:09 +03:00
2020-06-28 19:26:29 +03:00
switch opts.format {
case "org-mode":
2020-06-28 19:26:29 +03:00
return showOrgModeFormatter(env, snap)
case "json":
2020-06-28 19:26:29 +03:00
return showJsonFormatter(env, snap)
case "default":
2020-06-28 19:26:29 +03:00
return showDefaultFormatter(env, snap)
default:
2020-06-28 19:26:29 +03:00
return fmt.Errorf("unknown format %s", opts.format)
}
}
2020-06-28 19:26:29 +03:00
func showDefaultFormatter(env *Env, snapshot *bug.Snapshot) error {
// Header
2020-06-28 19:26:29 +03:00
env.out.Printf("%s [%s] %s\n\n",
colors.Cyan(snapshot.Id().Human()),
colors.Yellow(snapshot.Status),
snapshot.Title,
)
2020-06-28 19:26:29 +03:00
env.out.Printf("%s opened this issue %s\n",
colors.Magenta(snapshot.Author.DisplayName()),
snapshot.CreateTime.String(),
)
2020-06-28 19:26:29 +03:00
env.out.Printf("This was last edited at %s\n\n",
snapshot.EditTime().String(),
)
// Labels
var labels = make([]string, len(snapshot.Labels))
for i := range snapshot.Labels {
labels[i] = string(snapshot.Labels[i])
}
2020-06-28 19:26:29 +03:00
env.out.Printf("labels: %s\n",
strings.Join(labels, ", "),
)
// 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",
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",
strings.Join(participants, ", "),
)
// Comments
indent := " "
for i, comment := range snapshot.Comments {
var message string
env.out.Printf("%s%s #%d %s <%s>\n\n",
indent,
comment.Id().Human(),
i,
comment.Author.DisplayName(),
2019-01-19 18:01:06 +03:00
comment.Author.Email(),
2018-07-17 21:51:09 +03:00
)
if comment.Message == "" {
message = colors.BlackBold(colors.WhiteBg("No description provided."))
} else {
message = comment.Message
}
2020-06-28 19:26:29 +03:00
env.out.Printf("%s%s\n\n\n",
indent,
message,
)
2018-07-17 21:51:09 +03:00
}
return nil
}
type JSONBugSnapshot struct {
Id string `json:"id"`
HumanId string `json:"human_id"`
CreateTime JSONTime `json:"create_time"`
EditTime JSONTime `json:"edit_time"`
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"`
Comments []JSONComment `json:"comments"`
}
type JSONComment struct {
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"`
}
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 {
jsonBug := JSONBugSnapshot{
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-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-24 15:52:48 +03:00
jsonBug.Participants = make([]JSONIdentity, len(snapshot.Participants))
for i, element := range snapshot.Participants {
jsonBug.Participants[i] = NewJSONIdentity(element)
}
jsonBug.Comments = make([]JSONComment, len(snapshot.Comments))
for i, comment := range snapshot.Comments {
jsonBug.Comments[i] = NewJSONComment(comment)
}
jsonObject, _ := json.MarshalIndent(jsonBug, "", " ")
2020-06-28 19:26:29 +03:00
env.out.Printf("%s\n", jsonObject)
return nil
}
2020-06-28 19:26:29 +03:00
func showOrgModeFormatter(env *Env, snapshot *bug.Snapshot) error {
// Header
2020-06-28 19:26:29 +03:00
env.out.Printf("%s [%s] %s\n",
snapshot.Id().Human(),
snapshot.Status,
snapshot.Title,
)
2020-06-28 19:26:29 +03:00
env.out.Printf("* Author: %s\n",
snapshot.Author.DisplayName(),
)
2020-06-28 19:26:29 +03:00
env.out.Printf("* Creation Time: %s\n",
snapshot.CreateTime.String(),
)
2020-06-28 19:26:29 +03:00
env.out.Printf("* Last Edit: %s\n",
snapshot.EditTime().String(),
)
// 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")
if len(labels) > 0 {
2020-06-28 19:26:29 +03:00
env.out.Printf("** %s\n",
strings.Join(labels, "\n** "),
)
}
// 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",
strings.Join(actors, "\n** "),
)
// Participants
var participants = make([]string, len(snapshot.Participants))
for i, participant := range snapshot.Participants {
participants[i] = fmt.Sprintf("%s %s",
participant.Id().Human(),
participant.DisplayName(),
)
}
2020-06-28 19:26:29 +03:00
env.out.Printf("* Participants:\n** %s\n",
strings.Join(participants, "\n** "),
)
2020-06-28 19:26:29 +03:00
env.out.Printf("* Comments:\n")
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())
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-28 19:26:29 +03:00
env.out.Printf(": %s\n", message)
}
return nil
}