diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-24 14:52:48 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-24 15:10:57 +0200 |
commit | 1d06244c82b18959878cf199cd8dd500bbc46aa7 (patch) | |
tree | 0e4bf047c80545773d3a4967ba53531885411242 /commands | |
parent | ebd1030cdefb0fb1b65af4e856a9e62cba713ed6 (diff) | |
download | git-bug-1d06244c82b18959878cf199cd8dd500bbc46aa7.tar.gz |
cmds: cleanup and re-generate files
Diffstat (limited to 'commands')
-rw-r--r-- | commands/ls.go | 56 | ||||
-rw-r--r-- | commands/show.go | 146 | ||||
-rw-r--r-- | commands/user_ls.go | 84 |
3 files changed, 66 insertions, 220 deletions
diff --git a/commands/ls.go b/commands/ls.go index 7e3826fa..e1a7166a 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -95,18 +95,15 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) jsonBugs := make([]JSONBugExcerpt, len(bugExcerpts)) for i, b := range bugExcerpts { jsonBug := JSONBugExcerpt{ - b.Id.String(), - b.Id.Human(), - time.Unix(b.CreateUnixTime, 0), - time.Unix(b.EditUnixTime, 0), - b.Status.String(), - b.Labels, - b.Title, - []JSONIdentity{}, - []JSONIdentity{}, - JSONIdentity{}, - b.LenComments, - b.CreateMetadata, + Id: b.Id.String(), + HumanId: b.Id.Human(), + CreationTime: time.Unix(b.CreateUnixTime, 0), + LastEdited: time.Unix(b.EditUnixTime, 0), + Status: b.Status.String(), + Labels: b.Labels, + Title: b.Title, + Comments: b.LenComments, + Metadata: b.CreateMetadata, } if b.AuthorId != "" { @@ -114,46 +111,27 @@ func lsJsonFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) if err != nil { return err } - - i, err := NewJSONIdentity(author) - if err != nil { - return err - } - jsonBug.Author = i + jsonBug.Author = NewJSONIdentityFromExcerpt(author) } else { - i, err := NewJSONIdentity(b.LegacyAuthor) - if err != nil { - return err - } - jsonBug.Author = i + jsonBug.Author = NewJSONIdentityFromLegacyExcerpt(&b.LegacyAuthor) } - for _, element := range b.Actors { + jsonBug.Actors = make([]JSONIdentity, len(b.Actors)) + for i, element := range b.Actors { actor, err := backend.ResolveIdentityExcerpt(element) if err != nil { return err } - - i, err := NewJSONIdentity(actor) - if err != nil { - return err - } - - jsonBug.Actors = append(jsonBug.Actors, i) + jsonBug.Actors[i] = NewJSONIdentityFromExcerpt(actor) } - for _, element := range b.Participants { + jsonBug.Participants = make([]JSONIdentity, len(b.Participants)) + for i, element := range b.Participants { participant, err := backend.ResolveIdentityExcerpt(element) if err != nil { return err } - - i, err := NewJSONIdentity(participant) - if err != nil { - return err - } - - jsonBug.Participants = append(jsonBug.Participants, i) + jsonBug.Participants[i] = NewJSONIdentityFromExcerpt(participant) } jsonBugs[i] = jsonBug diff --git a/commands/show.go b/commands/show.go index ae5a15ce..4915f181 100644 --- a/commands/show.go +++ b/commands/show.go @@ -4,14 +4,16 @@ import ( "encoding/json" "errors" "fmt" + "strings" + "time" + + "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" - "strings" - "time" ) var ( @@ -82,8 +84,6 @@ func runShowBug(_ *cobra.Command, args []string) error { return showOrgmodeFormatter(snapshot) case "json": return showJsonFormatter(snapshot) - case "plain": - return showPlainFormatter(snapshot) case "default": return showDefaultFormatter(snapshot) default: @@ -165,83 +165,6 @@ func showDefaultFormatter(snapshot *bug.Snapshot) error { return nil } -func showPlainFormatter(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.CreatedAt.String(), - ) - - fmt.Printf("last edit: %s\n", - snapshot.LastEditTime().String(), - ) - - // Labels - var labels = make([]string, len(snapshot.Labels)) - for i := range snapshot.Labels { - labels[i] = string(snapshot.Labels[i]) - } - - fmt.Printf("labels: %s\n", - strings.Join(labels, ", "), - ) - - // Actors - var actors = make([]string, len(snapshot.Actors)) - for i := range snapshot.Actors { - actors[i] = snapshot.Actors[i].DisplayName() - } - - fmt.Printf("actors: %s\n", - strings.Join(actors, ", "), - ) - - // Participants - var participants = make([]string, len(snapshot.Participants)) - for i := range snapshot.Participants { - participants[i] = snapshot.Participants[i].DisplayName() - } - - fmt.Printf("participants: %s\n", - strings.Join(participants, ", "), - ) - - // Comments - indent := " " - - for i, comment := range snapshot.Comments { - var message string - fmt.Printf("%s#%d %s <%s>\n", - indent, - i, - comment.Author.DisplayName(), - comment.Author.Email(), - ) - - if comment.Message == "" { - message = "No description provided." - } else { - message = comment.Message - } - - fmt.Printf("%s%s\n", - indent, - strings.ReplaceAll(message, "\n", fmt.Sprintf("\n%s", indent)), - ) - } - - return nil -} - type JSONBugSnapshot struct { Id string `json:"id"` HumanId string `json:"human_id"` @@ -259,47 +182,31 @@ type JSONBugSnapshot struct { } type JSONComment struct { - Id int `json:"id"` - AuthorName string `json:"author_name"` - AuthorLogin string `json:"author_login"` - Message string `json:"message"` + Id int `json:"id"` + Author JSONIdentity `json:"author"` + Message string `json:"message"` } func showJsonFormatter(snapshot *bug.Snapshot) error { jsonBug := JSONBugSnapshot{ - snapshot.Id().String(), - snapshot.Id().Human(), - snapshot.CreatedAt, - snapshot.LastEditTime(), - snapshot.Status.String(), - snapshot.Labels, - snapshot.Title, - JSONIdentity{}, - []JSONIdentity{}, - []JSONIdentity{}, - []JSONComment{}, - } - - author, err := NewJSONIdentity(snapshot.Author) - if err != nil { - return err + Id: snapshot.Id().String(), + HumanId: snapshot.Id().Human(), + CreationTime: snapshot.CreatedAt, + LastEdited: snapshot.LastEditTime(), + Status: snapshot.Status.String(), + Labels: snapshot.Labels, + Title: snapshot.Title, + Author: NewJSONIdentity(snapshot.Author), } - jsonBug.Author = author - for _, element := range snapshot.Actors { - actor, err := NewJSONIdentity(element) - if err != nil { - return err - } - jsonBug.Actors = append(jsonBug.Actors, actor) + jsonBug.Actors = make([]JSONIdentity, len(snapshot.Actors)) + for i, element := range snapshot.Actors { + jsonBug.Actors[i] = NewJSONIdentity(element) } - for _, element := range snapshot.Participants { - participant, err := NewJSONIdentity(element) - if err != nil { - return err - } - jsonBug.Participants = append(jsonBug.Participants, participant) + jsonBug.Participants = make([]JSONIdentity, len(snapshot.Participants)) + for i, element := range snapshot.Participants { + jsonBug.Participants[i] = NewJSONIdentity(element) } for i, comment := range snapshot.Comments { @@ -310,10 +217,9 @@ func showJsonFormatter(snapshot *bug.Snapshot) error { message = comment.Message } jsonBug.Comments = append(jsonBug.Comments, JSONComment{ - i, - comment.Author.Name(), - comment.Author.Login(), - message, + Id: i, + Author: NewJSONIdentity(comment.Author), + Message: message, }) } @@ -417,5 +323,5 @@ func init() { 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,plain,json,org-mode]") + "Select the output formatting style. Valid values are [default,json,org-mode]") } diff --git a/commands/user_ls.go b/commands/user_ls.go index b0ebfdd5..78ef8258 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -2,14 +2,12 @@ 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/identity" "github.com/MichaelMure/git-bug/util/colors" "github.com/MichaelMure/git-bug/util/interrupt" ) @@ -37,12 +35,8 @@ func runUserLs(_ *cobra.Command, _ []string) error { } switch userLsOutputFormat { - case "org-mode": - return userLsOrgmodeFormatter(users) case "json": return userLsJsonFormatter(users) - case "plain": - return userLsPlainFormatter(users) case "default": return userLsDefaultFormatter(users) default: @@ -57,46 +51,29 @@ type JSONIdentity struct { Login string `json:"login"` } -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{ - "", - "", - 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 NewJSONIdentity(i identity.Interface) JSONIdentity { + return JSONIdentity{ + Id: i.Id().String(), + HumanId: i.Id().Human(), + Name: i.Name(), + Login: i.Login(), } } -func userLsPlainFormatter(users []*cache.IdentityExcerpt) error { - for _, user := range users { - fmt.Printf("%s %s\n", - user.Id.Human(), - user.DisplayName(), - ) +func NewJSONIdentityFromExcerpt(excerpt *cache.IdentityExcerpt) JSONIdentity { + return JSONIdentity{ + Id: excerpt.Id.String(), + HumanId: excerpt.Id.Human(), + Name: excerpt.Name, + Login: excerpt.Login, } +} - return nil +func NewJSONIdentityFromLegacyExcerpt(excerpt *cache.LegacyAuthorExcerpt) JSONIdentity { + return JSONIdentity{ + Name: excerpt.Name, + Login: excerpt.Login, + } } func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error { @@ -111,13 +88,9 @@ func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error { } 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) + jsonUsers := make([]JSONIdentity, len(users)) + for i, user := range users { + jsonUsers[i] = NewJSONIdentityFromExcerpt(user) } jsonObject, _ := json.MarshalIndent(jsonUsers, "", " ") @@ -125,17 +98,6 @@ func userLsJsonFormatter(users []*cache.IdentityExcerpt) error { return nil } -func userLsOrgmodeFormatter(users []*cache.IdentityExcerpt) error { - for _, user := range users { - fmt.Printf("* %s %s\n", - user.Id.Human(), - user.DisplayName(), - ) - } - - return nil -} - var userLsCmd = &cobra.Command{ Use: "ls", Short: "List identities.", @@ -147,5 +109,5 @@ func init() { userCmd.AddCommand(userLsCmd) userLsCmd.Flags().SortFlags = false userLsCmd.Flags().StringVarP(&userLsOutputFormat, "format", "f", "default", - "Select the output formatting style. Valid values are [default,plain,json,org-mode]") + "Select the output formatting style. Valid values are [default,json]") } |