aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user_ls.go
diff options
context:
space:
mode:
authorvince <vincetiu8@gmail.com>2020-06-21 13:51:48 +0800
committervince <vincetiu8@gmail.com>2020-06-21 13:51:48 +0800
commitcd8352edde7f4a26cb6b4dd922b05aa0bb23a70b (patch)
tree1ac2cf77cd24310350fe58cb358cb82d0b48da35 /commands/user_ls.go
parentf790083fb28ac0e68acddc9d5e7a709107a70bab (diff)
downloadgit-bug-cd8352edde7f4a26cb6b4dd922b05aa0bb23a70b.tar.gz
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
Diffstat (limited to 'commands/user_ls.go')
-rw-r--r--commands/user_ls.go67
1 files changed, 65 insertions, 2 deletions
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]")
}