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