From 5844dd0a6a08f496e6018c0bad0b38d82a8846fc Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 29 Dec 2022 12:49:12 +0100 Subject: commands: share JSON creation --- commands/bug/bug.go | 58 +++-------------------------------------------------- 1 file changed, 3 insertions(+), 55 deletions(-) (limited to 'commands/bug/bug.go') diff --git a/commands/bug/bug.go b/commands/bug/bug.go index bab040d8..a5ce11ed 100644 --- a/commands/bug/bug.go +++ b/commands/bug/bug.go @@ -1,7 +1,6 @@ package bugcmd import ( - "encoding/json" "fmt" "regexp" "strings" @@ -187,67 +186,16 @@ func repairQuery(args []string) string { return strings.Join(args, " ") } -type JSONBugExcerpt struct { - Id string `json:"id"` - HumanId string `json:"human_id"` - CreateTime cmdjson.Time `json:"create_time"` - EditTime cmdjson.Time `json:"edit_time"` - - Status string `json:"status"` - Labels []bug.Label `json:"labels"` - Title string `json:"title"` - Actors []cmdjson.Identity `json:"actors"` - Participants []cmdjson.Identity `json:"participants"` - Author cmdjson.Identity `json:"author"` - - Comments int `json:"comments"` - Metadata map[string]string `json:"metadata"` -} - func bugsJsonFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error { - jsonBugs := make([]JSONBugExcerpt, len(bugExcerpts)) + jsonBugs := make([]cmdjson.BugExcerpt, len(bugExcerpts)) for i, b := range bugExcerpts { - jsonBug := JSONBugExcerpt{ - Id: b.Id().String(), - HumanId: b.Id().Human(), - CreateTime: cmdjson.NewTime(b.CreateTime(), b.CreateLamportTime), - EditTime: cmdjson.NewTime(b.EditTime(), b.EditLamportTime), - Status: b.Status.String(), - Labels: b.Labels, - Title: b.Title, - Comments: b.LenComments, - Metadata: b.CreateMetadata, - } - - author, err := env.Backend.Identities().ResolveExcerpt(b.AuthorId) + jsonBug, err := cmdjson.NewBugExcerpt(env.Backend, b) if err != nil { return err } - jsonBug.Author = cmdjson.NewIdentityFromExcerpt(author) - - jsonBug.Actors = make([]cmdjson.Identity, len(b.Actors)) - for i, element := range b.Actors { - actor, err := env.Backend.Identities().ResolveExcerpt(element) - if err != nil { - return err - } - jsonBug.Actors[i] = cmdjson.NewIdentityFromExcerpt(actor) - } - - jsonBug.Participants = make([]cmdjson.Identity, len(b.Participants)) - for i, element := range b.Participants { - participant, err := env.Backend.Identities().ResolveExcerpt(element) - if err != nil { - return err - } - jsonBug.Participants[i] = cmdjson.NewIdentityFromExcerpt(participant) - } - jsonBugs[i] = jsonBug } - jsonObject, _ := json.MarshalIndent(jsonBugs, "", " ") - env.Out.Printf("%s\n", jsonObject) - return nil + return env.Out.PrintJSON(jsonBugs) } func bugsCompactFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error { -- cgit