1package commands
 2
 3import (
 4	"fmt"
 5	"os"
 6
 7	"github.com/MichaelMure/git-bug/cache"
 8	"github.com/MichaelMure/git-bug/util/interrupt"
 9	"github.com/spf13/cobra"
10)
11
12func runUserAdopt(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	prefix := args[0]
21
22	i, err := backend.ResolveIdentityPrefix(prefix)
23	if err != nil {
24		return err
25	}
26
27	err = backend.SetUserIdentity(i)
28	if err != nil {
29		return err
30	}
31
32	_, _ = fmt.Fprintf(os.Stderr, "Your identity is now: %s\n", i.DisplayName())
33
34	return nil
35}
36
37var userAdoptCmd = &cobra.Command{
38	Use:     "adopt <user-id>",
39	Short:   "Adopt an existing identity as your own.",
40	PreRunE: loadRepo,
41	RunE:    runUserAdopt,
42	Args:    cobra.ExactArgs(1),
43}
44
45func init() {
46	userCmd.AddCommand(userAdoptCmd)
47	userAdoptCmd.Flags().SortFlags = false
48}