From 304a3349300bba909b1f69e54c0d2d8a338994d1 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 24 Feb 2019 14:45:24 +0100 Subject: commands: add a "user adopt" command to use an existing identity --- commands/user_adopt.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 commands/user_adopt.go (limited to 'commands/user_adopt.go') diff --git a/commands/user_adopt.go b/commands/user_adopt.go new file mode 100644 index 00000000..5313e366 --- /dev/null +++ b/commands/user_adopt.go @@ -0,0 +1,48 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/util/interrupt" + "github.com/spf13/cobra" +) + +func runUserAdopt(cmd *cobra.Command, args []string) error { + backend, err := cache.NewRepoCache(repo) + if err != nil { + return err + } + defer backend.Close() + interrupt.RegisterCleaner(backend.Close) + + prefix := args[0] + + i, err := backend.ResolveIdentityPrefix(prefix) + if err != nil { + return err + } + + err = backend.SetUserIdentity(i) + if err != nil { + return err + } + + _, _ = fmt.Fprintf(os.Stderr, "Your identity is now: %s\n", i.DisplayName()) + + return nil +} + +var userAdoptCmd = &cobra.Command{ + Use: "adopt ", + Short: "Adopt an existing identity as your own.", + PreRunE: loadRepo, + RunE: runUserAdopt, + Args: cobra.ExactArgs(1), +} + +func init() { + userCmd.AddCommand(userAdoptCmd) + userAdoptCmd.Flags().SortFlags = false +} -- cgit