aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user_ls.go
blob: 353bc101154f1369bc3589989f143b95d2c923fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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"
)

var (
	userLsOutputFormat string
)

func runUserLs(_ *cobra.Command, _ []string) error {
	backend, err := cache.NewRepoCache(repo)
	if err != nil {
		return err
	}
	defer backend.Close()
	interrupt.RegisterCleaner(backend.Close)

	switch userLsOutputFormat {
	case "org-mode":
		return userLsOrgmodeFormatter(backend)
	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 {
			return err
		}

		fmt.Printf("%s %s\n",
			colors.Cyan(i.Id.Human()),
			i.DisplayName(),
		)
	}

	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
}

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.",
	PreRunE: loadRepo,
	RunE:    runUserLs,
}

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]")
}