From cd8352edde7f4a26cb6b4dd922b05aa0bb23a70b Mon Sep 17 00:00:00 2001 From: vince Date: Sun, 21 Jun 2020 13:51:48 +0800 Subject: Add output formatting support to the 'show' and 'user ls' commands This adds options to specify an output format for the commands in question. Supported formats are currently: - 'plain': plaintext, stripped of all colors - 'json': prints output as a json object --- commands/user_ls.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) (limited to 'commands/user_ls.go') diff --git a/commands/user_ls.go b/commands/user_ls.go index 609ff5a4..9d0eebf1 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -1,15 +1,19 @@ package commands import ( + "encoding/json" "fmt" - "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/util/colors" "github.com/MichaelMure/git-bug/util/interrupt" "github.com/spf13/cobra" ) -func runUserLs(cmd *cobra.Command, args []string) error { +var ( + userLsOutputFormat string +) + +func runUserLs(_ *cobra.Command, _ []string) error { backend, err := cache.NewRepoCache(repo) if err != nil { return err @@ -17,6 +21,42 @@ func runUserLs(cmd *cobra.Command, args []string) error { defer backend.Close() interrupt.RegisterCleaner(backend.Close) + switch userLsOutputFormat { + case "json": + return userLsJsonFormatter(backend) + case "plain": + return userLsPlainFormatter(backend) + case "default": + return userLsDefaultFormatter(backend) + default: + return fmt.Errorf("unknown format %s", userLsOutputFormat) + } +} + +type JSONIdentity struct { + Id string `json:"id"` + HumanId string `json:"human_id"` + Name string `json:"name"` + 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 + } + + 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 { @@ -32,6 +72,27 @@ func runUserLs(cmd *cobra.Command, args []string) error { 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.Human(), + i.Name, + i.Login, + }) + } + + jsonObject, _ := json.MarshalIndent(users, "", " ") + fmt.Printf("%s\n", jsonObject) + return nil +} + var userLsCmd = &cobra.Command{ Use: "ls", Short: "List identities.", @@ -42,4 +103,6 @@ var userLsCmd = &cobra.Command{ 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]") } -- cgit From fc3f6540b8fc941be76d01da49fcd43890ee7376 Mon Sep 17 00:00:00 2001 From: vince Date: Tue, 23 Jun 2020 17:51:42 +0800 Subject: Add org-mode formatting option This adds an option to the formatting flag on the ls, show and user ls commands that allows the user to specify the format of the output in org-mode. This will be useful for emacs users to read it in the editor. --- commands/user_ls.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'commands/user_ls.go') diff --git a/commands/user_ls.go b/commands/user_ls.go index 9d0eebf1..353bc101 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -22,6 +22,8 @@ func runUserLs(_ *cobra.Command, _ []string) error { interrupt.RegisterCleaner(backend.Close) switch userLsOutputFormat { + case "org-mode": + return userLsOrgmodeFormatter(backend) case "json": return userLsJsonFormatter(backend) case "plain": @@ -93,6 +95,22 @@ func userLsJsonFormatter(backend *cache.RepoCache) error { return nil } +func userLsOrgmodeFormatter(backend *cache.RepoCache) error { + for _, id := range backend.AllIdentityIds() { + i, err := backend.ResolveIdentityExcerpt(id) + if err != nil { + return err + } + + fmt.Printf("* %s %s\n", + i.Id.Human(), + i.DisplayName(), + ) + } + + return nil +} + var userLsCmd = &cobra.Command{ Use: "ls", Short: "List identities.", @@ -104,5 +122,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]") + "Select the output formatting style. Valid values are [default,plain,json,org-mode]") } -- cgit From 8eb004b405c4578b64fbb905b217b887b48ea800 Mon Sep 17 00:00:00 2001 From: vince Date: Wed, 24 Jun 2020 10:22:32 +0800 Subject: Clean up code and fix suggestions --- commands/user_ls.go | 111 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 43 deletions(-) (limited to 'commands/user_ls.go') 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(), ) } -- cgit From ebd1030cdefb0fb1b65af4e856a9e62cba713ed6 Mon Sep 17 00:00:00 2001 From: vince Date: Wed, 24 Jun 2020 14:34:36 +0800 Subject: Fix bugs This fixes some bugs experienced when using the new formatting options: - org-mode indents not working properly - print statements missing contents --- commands/user_ls.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'commands/user_ls.go') diff --git a/commands/user_ls.go b/commands/user_ls.go index 69286172..b0ebfdd5 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -78,8 +78,8 @@ func NewJSONIdentity(id interface{}) (JSONIdentity, error) { case cache.LegacyAuthorExcerpt: i := id.(cache.LegacyAuthorExcerpt) return JSONIdentity{ - nil, - nil, + "", + "", i.Name, i.Login, }, nil -- cgit From 1d06244c82b18959878cf199cd8dd500bbc46aa7 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Wed, 24 Jun 2020 14:52:48 +0200 Subject: cmds: cleanup and re-generate files --- commands/user_ls.go | 84 +++++++++++++++-------------------------------------- 1 file changed, 23 insertions(+), 61 deletions(-) (limited to 'commands/user_ls.go') 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]") } -- cgit From aab3a04d0c4ddbf97d589ba9048a539c6d7186e9 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 25 Jun 2020 23:18:17 +0200 Subject: bug: harmonize how time are used, fix some issues in command special formats This assume that the convertion from time.Time <--> Unix timestamp is lossless which seems to be. --- commands/user_ls.go | 33 --------------------------------- 1 file changed, 33 deletions(-) (limited to 'commands/user_ls.go') diff --git a/commands/user_ls.go b/commands/user_ls.go index 78ef8258..b3fb32e6 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -7,7 +7,6 @@ import ( "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/util/colors" "github.com/MichaelMure/git-bug/util/interrupt" ) @@ -44,38 +43,6 @@ func runUserLs(_ *cobra.Command, _ []string) error { } } -type JSONIdentity struct { - Id string `json:"id"` - HumanId string `json:"human_id"` - Name string `json:"name"` - Login string `json:"login"` -} - -func NewJSONIdentity(i identity.Interface) JSONIdentity { - return JSONIdentity{ - Id: i.Id().String(), - HumanId: i.Id().Human(), - Name: i.Name(), - Login: i.Login(), - } -} - -func NewJSONIdentityFromExcerpt(excerpt *cache.IdentityExcerpt) JSONIdentity { - return JSONIdentity{ - Id: excerpt.Id.String(), - HumanId: excerpt.Id.Human(), - Name: excerpt.Name, - Login: excerpt.Login, - } -} - -func NewJSONIdentityFromLegacyExcerpt(excerpt *cache.LegacyAuthorExcerpt) JSONIdentity { - return JSONIdentity{ - Name: excerpt.Name, - Login: excerpt.Login, - } -} - func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error { for _, user := range users { fmt.Printf("%s %s\n", -- cgit