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