aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorvince <vincetiu8@gmail.com>2020-06-24 10:22:32 +0800
committervince <vincetiu8@gmail.com>2020-06-24 10:22:32 +0800
commit8eb004b405c4578b64fbb905b217b887b48ea800 (patch)
tree073d3e4906ca36773fcedb8c177c282635c60949 /commands
parentfc3f6540b8fc941be76d01da49fcd43890ee7376 (diff)
downloadgit-bug-8eb004b405c4578b64fbb905b217b887b48ea800.tar.gz
Clean up code and fix suggestions
Diffstat (limited to 'commands')
-rw-r--r--commands/ls.go47
-rw-r--r--commands/show.go35
-rw-r--r--commands/user_ls.go111
3 files changed, 110 insertions, 83 deletions
diff --git a/commands/ls.go b/commands/ls.go
index 99bdbe82..7e3826fa 100644
--- a/commands/ls.go
+++ b/commands/ls.go
@@ -115,13 +115,17 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt)
return err
}
- jsonBug.Author.Name = author.DisplayName()
- jsonBug.Author.Login = author.Login
- jsonBug.Author.Id = author.Id.String()
- jsonBug.Author.HumanId = author.Id.Human()
+ i, err := NewJSONIdentity(author)
+ if err != nil {
+ return err
+ }
+ jsonBug.Author = i
} else {
- jsonBug.Author.Name = b.LegacyAuthor.DisplayName()
- jsonBug.Author.Login = b.LegacyAuthor.Login
+ i, err := NewJSONIdentity(b.LegacyAuthor)
+ if err != nil {
+ return err
+ }
+ jsonBug.Author = i
}
for _, element := range b.Actors {
@@ -130,12 +134,12 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt)
return err
}
- jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{
- actor.Id.String(),
- actor.Id.Human(),
- actor.Name,
- actor.Login,
- })
+ i, err := NewJSONIdentity(actor)
+ if err != nil {
+ return err
+ }
+
+ jsonBug.Actors = append(jsonBug.Actors, i)
}
for _, element := range b.Participants {
@@ -143,12 +147,13 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt)
if err != nil {
return err
}
- jsonBug.Participants = append(jsonBug.Participants, JSONIdentity{
- participant.Id.String(),
- participant.Id.Human(),
- participant.DisplayName(),
- participant.Login,
- })
+
+ i, err := NewJSONIdentity(participant)
+ if err != nil {
+ return err
+ }
+
+ jsonBug.Participants = append(jsonBug.Participants, i)
}
jsonBugs[i] = jsonBug
@@ -232,11 +237,9 @@ func lsOrgmodeFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerp
}
labels := b.Labels
- labelsString := ":"
+ var labelsString string
if len(labels) > 0 {
- for _, label := range labels {
- labelsString += label.String() + ":"
- }
+ labelsString = fmt.Sprintf(":%s:", strings.Replace(fmt.Sprint(labels), " ", ":", -1))
} else {
labelsString = ""
}
diff --git a/commands/show.go b/commands/show.go
index 101ed57d..4e6fb177 100644
--- a/commands/show.go
+++ b/commands/show.go
@@ -235,7 +235,7 @@ func showPlainFormatter(snapshot *bug.Snapshot) error {
fmt.Printf("%s%s\n",
indent,
- message,
+ strings.ReplaceAll(message, "\n", fmt.Sprintf("\n%s", indent)),
)
}
@@ -280,27 +280,26 @@ func showJsonFormatter(snapshot *bug.Snapshot) error {
[]JSONComment{},
}
- jsonBug.Author.Name = snapshot.Author.DisplayName()
- jsonBug.Author.Login = snapshot.Author.Login()
- jsonBug.Author.Id = snapshot.Author.Id().String()
- jsonBug.Author.HumanId = snapshot.Author.Id().Human()
+ author, err := NewJSONIdentity(snapshot.Author)
+ if err != nil {
+ return err
+ }
+ jsonBug.Author = author
for _, element := range snapshot.Actors {
- jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{
- element.Id().String(),
- element.Id().Human(),
- element.Name(),
- element.Login(),
- })
+ actor, err := NewJSONIdentity(element)
+ if err != nil {
+ return err
+ }
+ jsonBug.Actors = append(jsonBug.Actors, actor)
}
for _, element := range snapshot.Participants {
- jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{
- element.Id().String(),
- element.Id().Human(),
- element.Name(),
- element.Login(),
- })
+ participant, err := NewJSONIdentity(element)
+ if err != nil {
+ return err
+ }
+ jsonBug.Participants = append(jsonBug.Participants, participant)
}
for i, comment := range snapshot.Comments {
@@ -393,7 +392,7 @@ func showOrgmodeFormatter(snapshot *bug.Snapshot) error {
if comment.Message == "" {
message = "No description provided."
} else {
- message = strings.Replace(comment.Message, "\n", "\n: ", -1)
+ message = strings.ReplaceAll(comment.Message, "\n", "\n: ")
}
fmt.Printf(": %s\n",
diff --git a/commands/user_ls.go b/commands/user_ls.go
index 353bc101..69286172 100644
--- a/commands/user_ls.go
+++ b/commands/user_ls.go
@@ -2,11 +2,16 @@ package commands
import (
"encoding/json"
+ "errors"
"fmt"
+ "reflect"
+
+ "github.com/spf13/cobra"
+
"github.com/MichaelMure/git-bug/cache"
+ identity2 "github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/colors"
"github.com/MichaelMure/git-bug/util/interrupt"
- "github.com/spf13/cobra"
)
var (
@@ -21,15 +26,25 @@ func runUserLs(_ *cobra.Command, _ []string) error {
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
+ ids := backend.AllIdentityIds()
+ var users []*cache.IdentityExcerpt
+ for _, id := range ids {
+ user, err := backend.ResolveIdentityExcerpt(id)
+ if err != nil {
+ return err
+ }
+ users = append(users, user)
+ }
+
switch userLsOutputFormat {
case "org-mode":
- return userLsOrgmodeFormatter(backend)
+ return userLsOrgmodeFormatter(users)
case "json":
- return userLsJsonFormatter(backend)
+ return userLsJsonFormatter(users)
case "plain":
- return userLsPlainFormatter(backend)
+ return userLsPlainFormatter(users)
case "default":
- return userLsDefaultFormatter(backend)
+ return userLsDefaultFormatter(users)
default:
return fmt.Errorf("unknown format %s", userLsOutputFormat)
}
@@ -42,69 +57,79 @@ type JSONIdentity struct {
Login string `json:"login"`
}
-func userLsPlainFormatter(backend *cache.RepoCache) error {
- for _, id := range backend.AllIdentityIds() {
- i, err := backend.ResolveIdentityExcerpt(id)
- if err != nil {
- return err
- }
+func NewJSONIdentity(id interface{}) (JSONIdentity, error) {
+ switch id.(type) {
+ case *cache.IdentityExcerpt:
+ i := id.(*cache.IdentityExcerpt)
+ return JSONIdentity{
+ i.Id.String(),
+ i.Id.Human(),
+ i.Name,
+ i.Login,
+ }, nil
+ case identity2.Interface:
+ i := id.(identity2.Interface)
+ return JSONIdentity{
+ i.Id().String(),
+ i.Id().Human(),
+ i.Name(),
+ i.Login(),
+ }, nil
+ case cache.LegacyAuthorExcerpt:
+ i := id.(cache.LegacyAuthorExcerpt)
+ return JSONIdentity{
+ nil,
+ nil,
+ i.Name,
+ i.Login,
+ }, nil
+ default:
+ return JSONIdentity{}, errors.New(fmt.Sprintf("Inconvertible type, attempting to convert type %s to type %s.", reflect.TypeOf(id).String(), reflect.TypeOf(JSONIdentity{}).String()))
+ }
+}
+func userLsPlainFormatter(users []*cache.IdentityExcerpt) error {
+ for _, user := range users {
fmt.Printf("%s %s\n",
- i.Id.Human(),
- i.DisplayName(),
+ user.Id.Human(),
+ user.DisplayName(),
)
}
return nil
}
-func userLsDefaultFormatter(backend *cache.RepoCache) error {
- for _, id := range backend.AllIdentityIds() {
- i, err := backend.ResolveIdentityExcerpt(id)
- if err != nil {
- return err
- }
-
+func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error {
+ for _, user := range users {
fmt.Printf("%s %s\n",
- colors.Cyan(i.Id.Human()),
- i.DisplayName(),
+ colors.Cyan(user.Id.Human()),
+ user.DisplayName(),
)
}
return nil
}
-func userLsJsonFormatter(backend *cache.RepoCache) error {
- users := []JSONIdentity{}
- for _, id := range backend.AllIdentityIds() {
- i, err := backend.ResolveIdentityExcerpt(id)
+func userLsJsonFormatter(users []*cache.IdentityExcerpt) error {
+ jsonUsers := []JSONIdentity{}
+ for _, user := range users {
+ jsonUser, err := NewJSONIdentity(user)
if err != nil {
return err
}
-
- users = append(users, JSONIdentity{
- i.Id.String(),
- i.Id.Human(),
- i.Name,
- i.Login,
- })
+ jsonUsers = append(jsonUsers, jsonUser)
}
- jsonObject, _ := json.MarshalIndent(users, "", " ")
+ jsonObject, _ := json.MarshalIndent(jsonUsers, "", " ")
fmt.Printf("%s\n", jsonObject)
return nil
}
-func userLsOrgmodeFormatter(backend *cache.RepoCache) error {
- for _, id := range backend.AllIdentityIds() {
- i, err := backend.ResolveIdentityExcerpt(id)
- if err != nil {
- return err
- }
-
+func userLsOrgmodeFormatter(users []*cache.IdentityExcerpt) error {
+ for _, user := range users {
fmt.Printf("* %s %s\n",
- i.Id.Human(),
- i.DisplayName(),
+ user.Id.Human(),
+ user.DisplayName(),
)
}