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
|
package usercmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/git-bug/git-bug/cache"
"github.com/git-bug/git-bug/commands/cmdjson"
"github.com/git-bug/git-bug/commands/completion"
"github.com/git-bug/git-bug/commands/execenv"
"github.com/git-bug/git-bug/util/colors"
)
type userOptions struct {
format string
}
func NewUserCommand(env *execenv.Env) *cobra.Command {
options := userOptions{}
cmd := &cobra.Command{
Use: "user",
Short: "List identities",
PreRunE: execenv.LoadBackend(env),
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
return runUser(env, options)
}),
}
cmd.AddCommand(newUserNewCommand(env))
cmd.AddCommand(newUserShowCommand(env))
cmd.AddCommand(newUserAdoptCommand(env))
flags := cmd.Flags()
flags.SortFlags = false
flags.StringVarP(&options.format, "format", "f", "default",
"Select the output formatting style. Valid values are [default,json]")
cmd.RegisterFlagCompletionFunc("format", completion.From([]string{"default", "json"}))
return cmd
}
func runUser(env *execenv.Env, opts userOptions) error {
ids := env.Backend.Identities().AllIds()
var users []*cache.IdentityExcerpt
for _, id := range ids {
user, err := env.Backend.Identities().ResolveExcerpt(id)
if err != nil {
return err
}
users = append(users, user)
}
switch opts.format {
case "json":
return userJsonFormatter(env, users)
case "default":
return userDefaultFormatter(env, users)
default:
return fmt.Errorf("unknown format %s", opts.format)
}
}
func userDefaultFormatter(env *execenv.Env, users []*cache.IdentityExcerpt) error {
for _, user := range users {
env.Out.Printf("%s %s\n",
colors.Cyan(user.Id().Human()),
user.DisplayName(),
)
}
return nil
}
func userJsonFormatter(env *execenv.Env, users []*cache.IdentityExcerpt) error {
jsonUsers := make([]cmdjson.Identity, len(users))
for i, user := range users {
jsonUsers[i] = cmdjson.NewIdentityFromExcerpt(user)
}
return env.Out.PrintJSON(jsonUsers)
}
|