Clean up code and fix suggestions

vince created

Change summary

commands/ls.go      |  47 ++++++++++---------
commands/show.go    |  35 +++++++-------
commands/user_ls.go | 111 ++++++++++++++++++++++++++++------------------
3 files changed, 110 insertions(+), 83 deletions(-)

Detailed changes

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 = ""
 		}

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",

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(),
 		)
 	}