2019-02-17 18:12:06 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-02-23 15:01:46 +03:00
|
|
|
"os"
|
2019-02-17 18:12:06 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/input"
|
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func runUserCreate(cmd *cobra.Command, args []string) error {
|
|
|
|
backend, err := cache.NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer backend.Close()
|
|
|
|
interrupt.RegisterCleaner(backend.Close)
|
|
|
|
|
|
|
|
preName, err := backend.GetUserName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:06:42 +03:00
|
|
|
name, err := input.Prompt("Name", "name", preName, input.Required)
|
2019-02-17 18:12:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
preEmail, err := backend.GetUserEmail()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:06:42 +03:00
|
|
|
email, err := input.Prompt("Email", "email", preEmail, input.Required)
|
2019-02-17 18:12:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:06:42 +03:00
|
|
|
login, err := input.Prompt("Avatar URL", "avatar", "")
|
2019-02-17 18:12:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := backend.NewIdentityRaw(name, email, "", login, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = id.CommitAsNeeded()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:13:49 +03:00
|
|
|
set, err := backend.IsUserIdentitySet()
|
2019-02-17 18:12:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:13:49 +03:00
|
|
|
if !set {
|
|
|
|
err = backend.SetUserIdentity(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:01:46 +03:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr)
|
2019-02-17 18:12:06 +03:00
|
|
|
fmt.Println(id.Id())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var userCreateCmd = &cobra.Command{
|
|
|
|
Use: "create",
|
2019-02-24 16:46:08 +03:00
|
|
|
Short: "Create a new identity.",
|
2019-02-17 18:12:06 +03:00
|
|
|
PreRunE: loadRepo,
|
|
|
|
RunE: runUserCreate,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
userCmd.AddCommand(userCreateCmd)
|
|
|
|
userCreateCmd.Flags().SortFlags = false
|
|
|
|
}
|