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