2019-02-17 18:12:06 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-06-28 19:26:29 +03:00
|
|
|
"github.com/spf13/cobra"
|
2019-02-17 18:12:06 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/input"
|
|
|
|
)
|
|
|
|
|
2021-04-25 17:46:36 +03:00
|
|
|
type createUserOptions struct {
|
2021-05-09 12:14:45 +03:00
|
|
|
name string
|
|
|
|
email string
|
|
|
|
avatarURL string
|
|
|
|
nonInteractive bool
|
2021-04-25 17:46:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func newUserCreateCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
|
2021-04-25 17:46:36 +03:00
|
|
|
options := createUserOptions{}
|
2020-06-28 19:26:29 +03:00
|
|
|
cmd := &cobra.Command{
|
2021-05-09 12:33:20 +03:00
|
|
|
Use: "create",
|
|
|
|
Short: "Create a new identity.",
|
|
|
|
PreRunE: loadBackend(env),
|
|
|
|
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
|
2021-04-25 17:46:36 +03:00
|
|
|
return runUserCreate(env, options)
|
2021-05-09 12:33:20 +03:00
|
|
|
}),
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 17:46:36 +03:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVarP(&options.name, "name", "n", "", "Name to identify the user")
|
|
|
|
flags.StringVarP(&options.email, "email", "e", "", "Email of the user")
|
|
|
|
flags.StringVarP(&options.avatarURL, "avatar", "a", "", "Avatar URL")
|
2021-05-09 12:14:45 +03:00
|
|
|
flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
|
2021-04-25 17:46:36 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2021-04-25 17:46:36 +03:00
|
|
|
func runUserCreate(env *Env, opts createUserOptions) error {
|
2019-02-17 18:12:06 +03:00
|
|
|
|
2021-05-09 12:14:45 +03:00
|
|
|
if !opts.nonInteractive && opts.name == "" {
|
2021-04-25 17:46:36 +03:00
|
|
|
preName, err := env.backend.GetUserName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
opts.name, err = input.PromptDefault("Name", "name", preName, input.Required)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-09 12:14:45 +03:00
|
|
|
if !opts.nonInteractive && opts.email == "" {
|
2021-04-25 17:46:36 +03:00
|
|
|
preEmail, err := env.backend.GetUserEmail()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-17 18:12:06 +03:00
|
|
|
|
2021-04-25 17:46:36 +03:00
|
|
|
opts.email, err = input.PromptDefault("Email", "email", preEmail, input.Required)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-09 12:14:45 +03:00
|
|
|
if !opts.nonInteractive && opts.avatarURL == "" {
|
2021-04-25 17:46:36 +03:00
|
|
|
var err error
|
|
|
|
opts.avatarURL, err = input.Prompt("Avatar URL", "avatar")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 17:46:36 +03:00
|
|
|
id, err := env.backend.NewIdentityRaw(opts.name, opts.email, "", opts.avatarURL, nil, nil)
|
2019-02-17 18:12:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = id.CommitAsNeeded()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-28 20:09:32 +03:00
|
|
|
set, err := env.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 {
|
2020-06-28 20:09:32 +03:00
|
|
|
err = env.backend.SetUserIdentity(id)
|
2019-12-10 22:13:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.err.Println()
|
|
|
|
env.out.Println(id.Id())
|
2019-02-17 18:12:06 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|