Clean up code and fix suggestions

This commit is contained in:
vince 2020-06-24 10:22:32 +08:00
parent fc3f6540b8
commit 8eb004b405
3 changed files with 124 additions and 97 deletions

View File

@ -115,13 +115,17 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt)
return err return err
} }
jsonBug.Author.Name = author.DisplayName() i, err := NewJSONIdentity(author)
jsonBug.Author.Login = author.Login if err != nil {
jsonBug.Author.Id = author.Id.String() return err
jsonBug.Author.HumanId = author.Id.Human() }
jsonBug.Author = i
} else { } else {
jsonBug.Author.Name = b.LegacyAuthor.DisplayName() i, err := NewJSONIdentity(b.LegacyAuthor)
jsonBug.Author.Login = b.LegacyAuthor.Login if err != nil {
return err
}
jsonBug.Author = i
} }
for _, element := range b.Actors { for _, element := range b.Actors {
@ -130,12 +134,12 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt)
return err return err
} }
jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{ i, err := NewJSONIdentity(actor)
actor.Id.String(), if err != nil {
actor.Id.Human(), return err
actor.Name, }
actor.Login,
}) jsonBug.Actors = append(jsonBug.Actors, i)
} }
for _, element := range b.Participants { for _, element := range b.Participants {
@ -143,12 +147,13 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt)
if err != nil { if err != nil {
return err return err
} }
jsonBug.Participants = append(jsonBug.Participants, JSONIdentity{
participant.Id.String(), i, err := NewJSONIdentity(participant)
participant.Id.Human(), if err != nil {
participant.DisplayName(), return err
participant.Login, }
})
jsonBug.Participants = append(jsonBug.Participants, i)
} }
jsonBugs[i] = jsonBug jsonBugs[i] = jsonBug
@ -232,11 +237,9 @@ func lsOrgmodeFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerp
} }
labels := b.Labels labels := b.Labels
labelsString := ":" var labelsString string
if len(labels) > 0 { if len(labels) > 0 {
for _, label := range labels { labelsString = fmt.Sprintf(":%s:", strings.Replace(fmt.Sprint(labels), " ", ":", -1))
labelsString += label.String() + ":"
}
} else { } else {
labelsString = "" labelsString = ""
} }

View File

@ -235,7 +235,7 @@ func showPlainFormatter(snapshot *bug.Snapshot) error {
fmt.Printf("%s%s\n", fmt.Printf("%s%s\n",
indent, indent,
message, strings.ReplaceAll(message, "\n", fmt.Sprintf("\n%s", indent)),
) )
} }
@ -280,27 +280,26 @@ func showJsonFormatter(snapshot *bug.Snapshot) error {
[]JSONComment{}, []JSONComment{},
} }
jsonBug.Author.Name = snapshot.Author.DisplayName() author, err := NewJSONIdentity(snapshot.Author)
jsonBug.Author.Login = snapshot.Author.Login() if err != nil {
jsonBug.Author.Id = snapshot.Author.Id().String() return err
jsonBug.Author.HumanId = snapshot.Author.Id().Human() }
jsonBug.Author = author
for _, element := range snapshot.Actors { for _, element := range snapshot.Actors {
jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{ actor, err := NewJSONIdentity(element)
element.Id().String(), if err != nil {
element.Id().Human(), return err
element.Name(), }
element.Login(), jsonBug.Actors = append(jsonBug.Actors, actor)
})
} }
for _, element := range snapshot.Participants { for _, element := range snapshot.Participants {
jsonBug.Actors = append(jsonBug.Actors, JSONIdentity{ participant, err := NewJSONIdentity(element)
element.Id().String(), if err != nil {
element.Id().Human(), return err
element.Name(), }
element.Login(), jsonBug.Participants = append(jsonBug.Participants, participant)
})
} }
for i, comment := range snapshot.Comments { for i, comment := range snapshot.Comments {
@ -393,7 +392,7 @@ func showOrgmodeFormatter(snapshot *bug.Snapshot) error {
if comment.Message == "" { if comment.Message == "" {
message = "No description provided." message = "No description provided."
} else { } else {
message = strings.Replace(comment.Message, "\n", "\n: ", -1) message = strings.ReplaceAll(comment.Message, "\n", "\n: ")
} }
fmt.Printf(": %s\n", fmt.Printf(": %s\n",

View File

@ -2,11 +2,16 @@ package commands
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"reflect"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/cache" "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/colors"
"github.com/MichaelMure/git-bug/util/interrupt" "github.com/MichaelMure/git-bug/util/interrupt"
"github.com/spf13/cobra"
) )
var ( var (
@ -21,15 +26,25 @@ func runUserLs(_ *cobra.Command, _ []string) error {
defer backend.Close() defer backend.Close()
interrupt.RegisterCleaner(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 { switch userLsOutputFormat {
case "org-mode": case "org-mode":
return userLsOrgmodeFormatter(backend) return userLsOrgmodeFormatter(users)
case "json": case "json":
return userLsJsonFormatter(backend) return userLsJsonFormatter(users)
case "plain": case "plain":
return userLsPlainFormatter(backend) return userLsPlainFormatter(users)
case "default": case "default":
return userLsDefaultFormatter(backend) return userLsDefaultFormatter(users)
default: default:
return fmt.Errorf("unknown format %s", userLsOutputFormat) return fmt.Errorf("unknown format %s", userLsOutputFormat)
} }
@ -42,69 +57,79 @@ type JSONIdentity struct {
Login string `json:"login"` Login string `json:"login"`
} }
func userLsPlainFormatter(backend *cache.RepoCache) error { func NewJSONIdentity(id interface{}) (JSONIdentity, error) {
for _, id := range backend.AllIdentityIds() { switch id.(type) {
i, err := backend.ResolveIdentityExcerpt(id) case *cache.IdentityExcerpt:
if err != nil { i := id.(*cache.IdentityExcerpt)
return err return JSONIdentity{
}
fmt.Printf("%s %s\n",
i.Id.Human(),
i.DisplayName(),
)
}
return nil
}
func userLsDefaultFormatter(backend *cache.RepoCache) error {
for _, id := range backend.AllIdentityIds() {
i, err := backend.ResolveIdentityExcerpt(id)
if err != nil {
return err
}
fmt.Printf("%s %s\n",
colors.Cyan(i.Id.Human()),
i.DisplayName(),
)
}
return nil
}
func userLsJsonFormatter(backend *cache.RepoCache) error {
users := []JSONIdentity{}
for _, id := range backend.AllIdentityIds() {
i, err := backend.ResolveIdentityExcerpt(id)
if err != nil {
return err
}
users = append(users, JSONIdentity{
i.Id.String(), i.Id.String(),
i.Id.Human(), i.Id.Human(),
i.Name, i.Name,
i.Login, 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",
user.Id.Human(),
user.DisplayName(),
)
} }
jsonObject, _ := json.MarshalIndent(users, "", " ") return nil
}
func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error {
for _, user := range users {
fmt.Printf("%s %s\n",
colors.Cyan(user.Id.Human()),
user.DisplayName(),
)
}
return nil
}
func userLsJsonFormatter(users []*cache.IdentityExcerpt) error {
jsonUsers := []JSONIdentity{}
for _, user := range users {
jsonUser, err := NewJSONIdentity(user)
if err != nil {
return err
}
jsonUsers = append(jsonUsers, jsonUser)
}
jsonObject, _ := json.MarshalIndent(jsonUsers, "", " ")
fmt.Printf("%s\n", jsonObject) fmt.Printf("%s\n", jsonObject)
return nil return nil
} }
func userLsOrgmodeFormatter(backend *cache.RepoCache) error { func userLsOrgmodeFormatter(users []*cache.IdentityExcerpt) error {
for _, id := range backend.AllIdentityIds() { for _, user := range users {
i, err := backend.ResolveIdentityExcerpt(id)
if err != nil {
return err
}
fmt.Printf("* %s %s\n", fmt.Printf("* %s %s\n",
i.Id.Human(), user.Id.Human(),
i.DisplayName(), user.DisplayName(),
) )
} }