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