1package usercmd
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/spf13/cobra"
9
10 "github.com/MichaelMure/git-bug/cache"
11 "github.com/MichaelMure/git-bug/commands/completion"
12 "github.com/MichaelMure/git-bug/commands/execenv"
13)
14
15type userShowOptions struct {
16 fields string
17}
18
19func newUserShowCommand() *cobra.Command {
20 env := execenv.NewEnv()
21 options := userShowOptions{}
22
23 cmd := &cobra.Command{
24 Use: "user show [USER_ID]",
25 Short: "Display a user identity",
26 PreRunE: execenv.LoadBackendEnsureUser(env),
27 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
28 return runUserShow(env, options, args)
29 }),
30 ValidArgsFunction: completion.User(env),
31 }
32
33 flags := cmd.Flags()
34 flags.SortFlags = false
35
36 fields := []string{"email", "humanId", "id", "lastModification", "lastModificationLamports", "login", "metadata", "name"}
37 flags.StringVarP(&options.fields, "field", "f", "",
38 "Select field to display. Valid values are ["+strings.Join(fields, ",")+"]")
39 cmd.RegisterFlagCompletionFunc("field", completion.From(fields))
40
41 return cmd
42}
43
44func runUserShow(env *execenv.Env, opts userShowOptions, args []string) error {
45 if len(args) > 1 {
46 return errors.New("only one identity can be displayed at a time")
47 }
48
49 var id *cache.IdentityCache
50 var err error
51 if len(args) == 1 {
52 id, err = env.Backend.ResolveIdentityPrefix(args[0])
53 } else {
54 id, err = env.Backend.GetUserIdentity()
55 }
56
57 if err != nil {
58 return err
59 }
60
61 if opts.fields != "" {
62 switch opts.fields {
63 case "email":
64 env.Out.Printf("%s\n", id.Email())
65 case "login":
66 env.Out.Printf("%s\n", id.Login())
67 case "humanId":
68 env.Out.Printf("%s\n", id.Id().Human())
69 case "id":
70 env.Out.Printf("%s\n", id.Id())
71 case "lastModification":
72 env.Out.Printf("%s\n", id.LastModification().
73 Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
74 case "lastModificationLamport":
75 for name, t := range id.LastModificationLamports() {
76 env.Out.Printf("%s\n%d\n", name, t)
77 }
78 case "metadata":
79 for key, value := range id.ImmutableMetadata() {
80 env.Out.Printf("%s\n%s\n", key, value)
81 }
82 case "name":
83 env.Out.Printf("%s\n", id.Name())
84
85 default:
86 return fmt.Errorf("\nUnsupported field: %s\n", opts.fields)
87 }
88
89 return nil
90 }
91
92 env.Out.Printf("Id: %s\n", id.Id())
93 env.Out.Printf("Name: %s\n", id.Name())
94 env.Out.Printf("Email: %s\n", id.Email())
95 env.Out.Printf("Login: %s\n", id.Login())
96 env.Out.Printf("Last modification: %s\n", id.LastModification().Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
97 env.Out.Printf("Last moditication (lamport):\n")
98 for name, t := range id.LastModificationLamports() {
99 env.Out.Printf("\t%s: %d", name, t)
100 }
101 env.Out.Println("Metadata:")
102 for key, value := range id.ImmutableMetadata() {
103 env.Out.Printf(" %s --> %s\n", key, value)
104 }
105 // env.Out.Printf("Protected: %v\n", id.IsProtected())
106
107 return nil
108}