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(env *execenv.Env) *cobra.Command {
11 cmd := &cobra.Command{
12 Use: "adopt USER_ID",
13 Short: "Adopt an existing identity as your own",
14 Args: cobra.ExactArgs(1),
15 PreRunE: execenv.LoadBackend(env),
16 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
17 return runUserAdopt(env, args)
18 }),
19 ValidArgsFunction: completion.User(env),
20 }
21
22 return cmd
23}
24
25func runUserAdopt(env *execenv.Env, args []string) error {
26 prefix := args[0]
27
28 i, err := env.Backend.Identities().ResolvePrefix(prefix)
29 if err != nil {
30 return err
31 }
32
33 err = env.Backend.SetUserIdentity(i)
34 if err != nil {
35 return err
36 }
37
38 env.Out.Printf("Your identity is now: %s\n", i.DisplayName())
39
40 return nil
41}