aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user/user_adopt.go
blob: a5fe68c5887e41a9c0d5ea5146aefdacc21b7a2b (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
41
package usercmd

import (
	"github.com/spf13/cobra"

	"github.com/git-bug/git-bug/commands/completion"
	"github.com/git-bug/git-bug/commands/execenv"
)

func newUserAdoptCommand(env *execenv.Env) *cobra.Command {
	cmd := &cobra.Command{
		Use:     "adopt USER_ID",
		Short:   "Adopt an existing identity as your own",
		Args:    cobra.ExactArgs(1),
		PreRunE: execenv.LoadBackend(env),
		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
			return runUserAdopt(env, args)
		}),
		ValidArgsFunction: completion.User(env),
	}

	return cmd
}

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

	i, err := env.Backend.Identities().ResolvePrefix(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
}