user_adopt.go

 1package usercmd
 2
 3import (
 4	"github.com/spf13/cobra"
 5
 6	"github.com/MichaelMure/git-bug/commands/completion"
 7	"github.com/MichaelMure/git-bug/commands/execenv"
 8)
 9
10func newUserAdoptCommand() *cobra.Command {
11	env := execenv.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: execenv.LoadBackend(env),
18		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
19			return runUserAdopt(env, args)
20		}),
21		ValidArgsFunction: completion.User(env),
22	}
23
24	return cmd
25}
26
27func runUserAdopt(env *execenv.Env, args []string) error {
28	prefix := args[0]
29
30	i, err := env.Backend.ResolveIdentityPrefix(prefix)
31	if err != nil {
32		return err
33	}
34
35	err = env.Backend.SetUserIdentity(i)
36	if err != nil {
37		return err
38	}
39
40	env.Out.Printf("Your identity is now: %s\n", i.DisplayName())
41
42	return nil
43}