aboutsummaryrefslogtreecommitdiffstats
path: root/commands/show.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-06-26 01:02:27 +0200
committerGitHub <noreply@github.com>2020-06-26 01:02:27 +0200
commit2dd0dbb1344ae9293aae05346f977b5d5907934b (patch)
tree610ff7e381ef99827dc1b1dbc9ae4ea137fe5e21 /commands/show.go
parentf790083fb28ac0e68acddc9d5e7a709107a70bab (diff)
parentc326007d01b623bcf883f74f31fc3e5ab3078396 (diff)
downloadgit-bug-2dd0dbb1344ae9293aae05346f977b5d5907934b.tar.gz
Merge pull request #410 from MichaelMure/output-formatting-2
Add formatting options to the 'show' and 'user ls' commands
Diffstat (limited to 'commands/show.go')
-rw-r--r--commands/show.go198
1 files changed, 183 insertions, 15 deletions
diff --git a/commands/show.go b/commands/show.go
index 0bb3dc4a..2f4e46ed 100644
--- a/commands/show.go
+++ b/commands/show.go
@@ -1,22 +1,26 @@
package commands
import (
+ "encoding/json"
"errors"
"fmt"
"strings"
+ "github.com/spf13/cobra"
+
+ "github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
_select "github.com/MichaelMure/git-bug/commands/select"
"github.com/MichaelMure/git-bug/util/colors"
"github.com/MichaelMure/git-bug/util/interrupt"
- "github.com/spf13/cobra"
)
var (
- showFieldsQuery string
+ showFieldsQuery string
+ showOutputFormat string
)
-func runShowBug(cmd *cobra.Command, args []string) error {
+func runShowBug(_ *cobra.Command, args []string) error {
backend, err := cache.NewRepoCache(repo)
if err != nil {
return err
@@ -35,16 +39,16 @@ func runShowBug(cmd *cobra.Command, args []string) error {
return errors.New("invalid bug: no comment")
}
- firstComment := snapshot.Comments[0]
-
if showFieldsQuery != "" {
switch showFieldsQuery {
case "author":
- fmt.Printf("%s\n", firstComment.Author.DisplayName())
+ fmt.Printf("%s\n", snapshot.Author.DisplayName())
case "authorEmail":
- fmt.Printf("%s\n", firstComment.Author.Email())
+ fmt.Printf("%s\n", snapshot.Author.Email())
case "createTime":
- fmt.Printf("%s\n", firstComment.FormatTime())
+ fmt.Printf("%s\n", snapshot.CreateTime.String())
+ case "lastEdit":
+ fmt.Printf("%s\n", snapshot.EditTime().String())
case "humanId":
fmt.Printf("%s\n", snapshot.Id().Human())
case "id":
@@ -74,16 +78,33 @@ func runShowBug(cmd *cobra.Command, args []string) error {
return nil
}
+ switch showOutputFormat {
+ case "org-mode":
+ return showOrgmodeFormatter(snapshot)
+ case "json":
+ return showJsonFormatter(snapshot)
+ case "default":
+ return showDefaultFormatter(snapshot)
+ default:
+ return fmt.Errorf("unknown format %s", showOutputFormat)
+ }
+}
+
+func showDefaultFormatter(snapshot *bug.Snapshot) error {
// Header
- fmt.Printf("[%s] %s %s\n\n",
- colors.Yellow(snapshot.Status),
+ fmt.Printf("%s [%s] %s\n\n",
colors.Cyan(snapshot.Id().Human()),
+ colors.Yellow(snapshot.Status),
snapshot.Title,
)
- fmt.Printf("%s opened this issue %s\n\n",
- colors.Magenta(firstComment.Author.DisplayName()),
- firstComment.FormatTimeRel(),
+ fmt.Printf("%s opened this issue %s\n",
+ colors.Magenta(snapshot.Author.DisplayName()),
+ snapshot.CreateTime.String(),
+ )
+
+ fmt.Printf("This was last edited at %s\n\n",
+ snapshot.EditTime().String(),
)
// Labels
@@ -143,6 +164,151 @@ func runShowBug(cmd *cobra.Command, args []string) error {
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"`
+ 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,
+ }
+}
+
+func showJsonFormatter(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),
+ }
+
+ jsonBug.Actors = make([]JSONIdentity, len(snapshot.Actors))
+ for i, element := range snapshot.Actors {
+ jsonBug.Actors[i] = NewJSONIdentity(element)
+ }
+
+ 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, "", " ")
+ fmt.Printf("%s\n", jsonObject)
+
+ return nil
+}
+
+func showOrgmodeFormatter(snapshot *bug.Snapshot) error {
+ // Header
+ fmt.Printf("%s [%s] %s\n",
+ snapshot.Id().Human(),
+ snapshot.Status,
+ snapshot.Title,
+ )
+
+ fmt.Printf("* Author: %s\n",
+ snapshot.Author.DisplayName(),
+ )
+
+ fmt.Printf("* Creation Time: %s\n",
+ snapshot.CreateTime.String(),
+ )
+
+ fmt.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)
+ }
+
+ fmt.Printf("* Labels:\n")
+ if len(labels) > 0 {
+ fmt.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(),
+ )
+ }
+
+ fmt.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(),
+ )
+ }
+
+ fmt.Printf("* Participants:\n** %s\n",
+ strings.Join(participants, "\n** "),
+ )
+
+ fmt.Printf("* Comments:\n")
+
+ for i, comment := range snapshot.Comments {
+ var message string
+ fmt.Printf("** #%d %s\n",
+ i,
+ comment.Author.DisplayName(),
+ )
+
+ if comment.Message == "" {
+ message = "No description provided."
+ } else {
+ message = strings.ReplaceAll(comment.Message, "\n", "\n: ")
+ }
+
+ fmt.Printf(": %s\n",
+ message,
+ )
+ }
+
+ return nil
+}
+
var showCmd = &cobra.Command{
Use: "show [<id>]",
Short: "Display the details of a bug.",
@@ -152,6 +318,8 @@ var showCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(showCmd)
- showCmd.Flags().StringVarP(&showFieldsQuery, "field", "f", "",
- "Select field to display. Valid values are [author,authorEmail,createTime,humanId,id,labels,shortId,status,title,actors,participants]")
+ showCmd.Flags().StringVarP(&showFieldsQuery, "field", "", "",
+ "Select field to display. Valid values are [author,authorEmail,createTime,lastEdit,humanId,id,labels,shortId,status,title,actors,participants]")
+ showCmd.Flags().StringVarP(&showOutputFormat, "format", "f", "default",
+ "Select the output formatting style. Valid values are [default,json,org-mode]")
}