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
12func runUser(cmd *cobra.Command, args []string) error {
13 backend, err := cache.NewRepoCache(repo)
14 if err != nil {
15 return err
16 }
17 defer backend.Close()
18 interrupt.RegisterCleaner(backend.Close)
19
20 if len(args) > 1 {
21 return errors.New("only one identity can be displayed at a time")
22 }
23
24 var id *cache.IdentityCache
25 if len(args) == 1 {
26 id, err = backend.ResolveIdentityPrefix(args[0])
27 } else {
28 id, err = backend.GetUserIdentity()
29 }
30
31 if err != nil {
32 return err
33 }
34
35 fmt.Printf("Id: %s\n", id.Id())
36 fmt.Printf("Name: %s\n", id.Name())
37 fmt.Printf("Login: %s\n", id.Login())
38 fmt.Printf("Email: %s\n", id.Email())
39 fmt.Printf("Last modification: %s (lamport %d)\n",
40 id.LastModification().Time().Format("Mon Jan 2 15:04:05 2006 +0200"),
41 id.LastModificationLamport())
42 fmt.Println("Metadata:")
43 for key, value := range id.ImmutableMetadata() {
44 fmt.Printf(" %s --> %s\n", key, value)
45 }
46 // fmt.Printf("Protected: %v\n", id.IsProtected())
47
48 return nil
49}
50
51var userCmd = &cobra.Command{
52 Use: "user [<user-id>]",
53 Short: "Display or change the user identity.",
54 PreRunE: loadRepo,
55 RunE: runUser,
56}
57
58func init() {
59 RootCmd.AddCommand(userCmd)
60 userCmd.Flags().SortFlags = false
61}