aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user_adopt.go
blob: 521f032ff5a9384f501f4cce4769f38af399acba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package commands

import (
	"github.com/spf13/cobra"
)

func newUserAdoptCommand() *cobra.Command {
	env := newEnv()

	cmd := &cobra.Command{
		Use:      "adopt <user-id>",
		Short:    "Adopt an existing identity as your own.",
		Args:     cobra.ExactArgs(1),
		PreRunE:  loadBackend(env),
		PostRunE: closeBackend(env),
		RunE: func(cmd *cobra.Command, args []string) error {
			return runUserAdopt(env, args)
		},
	}

	return cmd
}

func runUserAdopt(env *Env, args []string) error {
	prefix := args[0]

	i, err := env.backend.ResolveIdentityPrefix(prefix)
	if err != nil {
		return err
	}

	err = env.backend.SetUserIdentity(i)
	if err != nil {
		return err
	}

	env.out.Printf("Your identity is now: %s\n", i.DisplayName())

	return nil
}