user_adopt.go

 1package commands
 2
 3import (
 4	"github.com/spf13/cobra"
 5
 6	"github.com/MichaelMure/git-bug/cache"
 7	"github.com/MichaelMure/git-bug/util/interrupt"
 8)
 9
10func newUserAdoptCommand() *cobra.Command {
11	env := newEnv()
12
13	cmd := &cobra.Command{
14		Use:     "adopt <user-id>",
15		Short:   "Adopt an existing identity as your own.",
16		Args:    cobra.ExactArgs(1),
17		PreRunE: loadRepo(env),
18		RunE: func(cmd *cobra.Command, args []string) error {
19			return runUserAdopt(env, args)
20		},
21	}
22
23	return cmd
24}
25
26func runUserAdopt(env *Env, args []string) error {
27	backend, err := cache.NewRepoCache(env.repo)
28	if err != nil {
29		return err
30	}
31	defer backend.Close()
32	interrupt.RegisterCleaner(backend.Close)
33
34	prefix := args[0]
35
36	i, err := backend.ResolveIdentityPrefix(prefix)
37	if err != nil {
38		return err
39	}
40
41	err = backend.SetUserIdentity(i)
42	if err != nil {
43		return err
44	}
45
46	env.out.Printf("Your identity is now: %s\n", i.DisplayName())
47
48	return nil
49}