1package commands
 2
 3import (
 4	"github.com/spf13/cobra"
 5)
 6
 7func newUserAdoptCommand() *cobra.Command {
 8	env := newEnv()
 9
10	cmd := &cobra.Command{
11		Use:     "adopt USER-ID",
12		Short:   "Adopt an existing identity as your own.",
13		Args:    cobra.ExactArgs(1),
14		PreRunE: loadBackend(env),
15		RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
16			return runUserAdopt(env, args)
17		}),
18		ValidArgsFunction: completeUser(env),
19	}
20
21	return cmd
22}
23
24func runUserAdopt(env *Env, args []string) error {
25	prefix := args[0]
26
27	i, err := env.backend.ResolveIdentityPrefix(prefix)
28	if err != nil {
29		return err
30	}
31
32	err = env.backend.SetUserIdentity(i)
33	if err != nil {
34		return err
35	}
36
37	env.out.Printf("Your identity is now: %s\n", i.DisplayName())
38
39	return nil
40}