user_key.go

 1package commands
 2
 3import (
 4	"fmt"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/MichaelMure/git-bug/cache"
 9	"github.com/MichaelMure/git-bug/util/interrupt"
10)
11
12func ResolveUser(repo *cache.RepoCache, args []string) (*cache.IdentityCache, []string, error) {
13	var err error
14	var id *cache.IdentityCache
15	if len(args) > 0 {
16		id, err = repo.ResolveIdentityPrefix(args[0])
17		args = args[1:]
18	} else {
19		id, err = repo.GetUserIdentity()
20	}
21	return id, args, err
22}
23
24func runKey(cmd *cobra.Command, args []string) error {
25	backend, err := cache.NewRepoCache(repo)
26	if err != nil {
27		return err
28	}
29	defer backend.Close()
30	interrupt.RegisterCleaner(backend.Close)
31
32	id, args, err := ResolveUser(backend, args)
33	if err != nil {
34		return err
35	}
36
37	if len(args) > 0 {
38		return fmt.Errorf("unexpected arguments: %s", args)
39	}
40
41	for _, key := range id.Keys() {
42		fmt.Println(key.Fingerprint())
43	}
44
45	return nil
46}
47
48var keyCmd = &cobra.Command{
49	Use:     "key [<user-id>]",
50	Short:   "Display, add or remove keys to/from a user.",
51	PreRunE: loadRepoEnsureUser,
52	RunE:    runKey,
53}
54
55func init() {
56	userCmd.AddCommand(keyCmd)
57}