commands: add a "user adopt" command to use an existing identity

Michael Muré created

Change summary

commands/user_adopt.go       | 48 ++++++++++++++++++++++++++++++++++++++
commands/user_create.go      |  4 +++
doc/man/git-bug-user-adopt.1 | 29 ++++++++++++++++++++++
doc/man/git-bug-user.1       |  2 
doc/md/git-bug_user_adopt.md | 22 +++++++++++++++++
misc/bash_completion/git-bug | 21 ++++++++++++++++
misc/zsh_completion/git-bug  |  2 
7 files changed, 126 insertions(+), 2 deletions(-)

Detailed changes

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 <id>",
+	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
+}

commands/user_create.go 🔗

@@ -18,6 +18,10 @@ func runUserCreate(cmd *cobra.Command, args []string) error {
 	defer backend.Close()
 	interrupt.RegisterCleaner(backend.Close)
 
+	_, _ = fmt.Fprintf(os.Stderr, "Before creating a new identity, please be aware that "+
+		"you can also use an already existing one using \"git bug user adopt\". As an example, "+
+		"you can do that if your identity has already been created by an importer.\n\n")
+
 	preName, err := backend.GetUserName()
 	if err != nil {
 		return err

doc/man/git-bug-user-adopt.1 🔗

@@ -0,0 +1,29 @@
+.TH "GIT-BUG" "1" "Feb 2019" "Generated from git-bug's source code" "" 
+.nh
+.ad l
+
+
+.SH NAME
+.PP
+git\-bug\-user\-adopt \- Adopt an existing identity as your own.
+
+
+.SH SYNOPSIS
+.PP
+\fBgit\-bug user adopt <id> [flags]\fP
+
+
+.SH DESCRIPTION
+.PP
+Adopt an existing identity as your own.
+
+
+.SH OPTIONS
+.PP
+\fB\-h\fP, \fB\-\-help\fP[=false]
+    help for adopt
+
+
+.SH SEE ALSO
+.PP
+\fBgit\-bug\-user(1)\fP

doc/man/git-bug-user.1 🔗

@@ -26,4 +26,4 @@ Display or change the user identity
 
 .SH SEE ALSO
 .PP
-\fBgit\-bug(1)\fP, \fBgit\-bug\-user\-create(1)\fP, \fBgit\-bug\-user\-ls(1)\fP
+\fBgit\-bug(1)\fP, \fBgit\-bug\-user\-adopt(1)\fP, \fBgit\-bug\-user\-create(1)\fP, \fBgit\-bug\-user\-ls(1)\fP

doc/md/git-bug_user_adopt.md 🔗

@@ -0,0 +1,22 @@
+## git-bug user adopt
+
+Adopt an existing identity as your own.
+
+### Synopsis
+
+Adopt an existing identity as your own.
+
+```
+git-bug user adopt <id> [flags]
+```
+
+### Options
+
+```
+  -h, --help   help for adopt
+```
+
+### SEE ALSO
+
+* [git-bug user](git-bug_user.md)	 - Display or change the user identity.
+

misc/bash_completion/git-bug 🔗

@@ -799,6 +799,26 @@ _git-bug_title()
     noun_aliases=()
 }
 
+_git-bug_user_adopt()
+{
+    last_command="git-bug_user_adopt"
+
+    command_aliases=()
+
+    commands=()
+
+    flags=()
+    two_word_flags=()
+    local_nonpersistent_flags=()
+    flags_with_completion=()
+    flags_completion=()
+
+
+    must_have_one_flag=()
+    must_have_one_noun=()
+    noun_aliases=()
+}
+
 _git-bug_user_create()
 {
     last_command="git-bug_user_create"
@@ -849,6 +869,7 @@ _git-bug_user()
     command_aliases=()
 
     commands=()
+    commands+=("adopt")
     commands+=("create")
     commands+=("ls")
 

misc/zsh_completion/git-bug 🔗

@@ -33,7 +33,7 @@ case $state in
         _arguments '2: :(edit)'
       ;;
       user)
-        _arguments '2: :(create ls)'
+        _arguments '2: :(adopt create ls)'
       ;;
       *)
         _arguments '*: :_files'