user.go

 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		// TODO
27		return errors.New("this is not working yet, cache need to be hacked on")
28		id, err = backend.ResolveIdentityPrefix(args[0])
29	} else {
30		id, err = backend.GetUserIdentity()
31	}
32
33	if err != nil {
34		return err
35	}
36
37	fmt.Printf("Id: %s\n", id.Id())
38	fmt.Printf("Name: %s\n", id.Name())
39	fmt.Printf("Login: %s\n", id.Login())
40	fmt.Printf("Email: %s\n", id.Email())
41	fmt.Printf("Protected: %v\n", id.IsProtected())
42
43	return nil
44}
45
46var userCmd = &cobra.Command{
47	Use:     "user [<id>]",
48	Short:   "Display or change the user identity",
49	PreRunE: loadRepo,
50	RunE:    runUser,
51}
52
53func init() {
54	RootCmd.AddCommand(userCmd)
55	userCmd.Flags().SortFlags = false
56}