2019-02-24 16:45:24 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-12-26 00:55:53 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core/auth"
|
2019-02-24 16:45:24 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2019-12-26 00:55:53 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2019-02-24 16:45:24 +03:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2019-12-26 00:55:53 +03:00
|
|
|
_, err = backend.GetUserIdentity()
|
|
|
|
if err == identity.ErrNoIdentitySet {
|
|
|
|
err = auth.ReplaceDefaultUser(repo, i.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:45:24 +03:00
|
|
|
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{
|
2019-03-23 21:03:39 +03:00
|
|
|
Use: "adopt <user-id>",
|
2019-02-24 16:45:24 +03:00
|
|
|
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
|
|
|
|
}
|