1package commands
2
3import (
4 "fmt"
5
6 "github.com/pkg/errors"
7 "github.com/spf13/cobra"
8
9 "github.com/MichaelMure/git-bug/cache"
10 "github.com/MichaelMure/git-bug/identity"
11 "github.com/MichaelMure/git-bug/util/interrupt"
12)
13
14func runKeyRm(cmd *cobra.Command, args []string) error {
15 backend, err := cache.NewRepoCache(repo)
16 if err != nil {
17 return err
18 }
19 defer backend.Close()
20 interrupt.RegisterCleaner(backend.Close)
21
22 if len(args) == 0 {
23 return errors.New("missing key fingerprint")
24 }
25
26 fingerprint := args[0]
27 args = args[1:]
28
29 id, args, err := ResolveUser(backend, args)
30 if err != nil {
31 return err
32 }
33
34 if len(args) > 0 {
35 return fmt.Errorf("unexpected arguments: %s", args)
36 }
37
38 var removedKey *identity.Key
39 err = id.Mutate(func(mutator identity.Mutator) identity.Mutator {
40 for j, key := range mutator.Keys {
41 if key.Fingerprint() == fingerprint {
42 removedKey = key
43 copy(mutator.Keys[j:], mutator.Keys[j+1:])
44 mutator.Keys = mutator.Keys[:len(mutator.Keys)-1]
45 break
46 }
47 }
48 return mutator
49 })
50
51 if err != nil {
52 return err
53 }
54
55 if removedKey == nil {
56 return errors.New("key not found")
57 }
58
59 return id.Commit()
60}
61
62var keyRmCmd = &cobra.Command{
63 Use: "rm <key-fingerprint> [<user-id>]",
64 Short: "Remove a PGP key from the adopted or the specified user.",
65 PreRunE: loadRepoEnsureUser,
66 RunE: runKeyRm,
67}
68
69func init() {
70 keyCmd.AddCommand(keyRmCmd)
71}