aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user.go
blob: 55ad813ca8d03239190a5a040eea1576c66dcc14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package commands

import (
	"errors"
	"fmt"

	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/util/interrupt"
	"github.com/spf13/cobra"
)

func runUser(cmd *cobra.Command, args []string) error {
	backend, err := cache.NewRepoCache(repo)
	if err != nil {
		return err
	}
	defer backend.Close()
	interrupt.RegisterCleaner(backend.Close)

	if len(args) > 1 {
		return errors.New("only one identity can be displayed at a time")
	}

	var id *cache.IdentityCache
	if len(args) == 1 {
		// TODO
		return errors.New("this is not working yet, cache need to be hacked on")
		id, err = backend.ResolveIdentityPrefix(args[0])
	} else {
		id, err = backend.GetUserIdentity()
	}

	if err != nil {
		return err
	}

	fmt.Printf("Id: %s\n", id.Id())
	fmt.Printf("Name: %s\n", id.Name())
	fmt.Printf("Login: %s\n", id.Login())
	fmt.Printf("Email: %s\n", id.Email())
	fmt.Printf("Protected: %v\n", id.IsProtected())

	return nil
}

var userCmd = &cobra.Command{
	Use:     "user [<id>]",
	Short:   "Display or change the user identity",
	PreRunE: loadRepo,
	RunE:    runUser,
}

func init() {
	RootCmd.AddCommand(userCmd)
	userCmd.Flags().SortFlags = false
}