aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user_ls.go
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/user_ls.go
parentfc3f6540b8fc941be76d01da49fcd43890ee7376 (diff)
downloadgit-bug-8eb004b405c4578b64fbb905b217b887b48ea800.tar.gz
Clean up code and fix suggestions
Diffstat (limited to 'commands/user_ls.go')
-rw-r--r--commands/user_ls.go111
1 files changed, 68 insertions, 43 deletions
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(),
)
}