user_create.go

 1package commands
 2
 3import (
 4	"fmt"
 5
 6	"github.com/MichaelMure/git-bug/cache"
 7	"github.com/MichaelMure/git-bug/input"
 8	"github.com/MichaelMure/git-bug/util/interrupt"
 9	"github.com/spf13/cobra"
10)
11
12func runUserCreate(cmd *cobra.Command, args []string) error {
13	backend, err := cache.NewRepoCache(repo)
14	if err != nil {
15		return err
16	}
17	defer backend.Close()
18	interrupt.RegisterCleaner(backend.Close)
19
20	preName, err := backend.GetUserName()
21	if err != nil {
22		return err
23	}
24
25	name, err := input.PromptValueRequired("Name", preName)
26	if err != nil {
27		return err
28	}
29
30	preEmail, err := backend.GetUserEmail()
31	if err != nil {
32		return err
33	}
34
35	email, err := input.PromptValueRequired("Email", preEmail)
36	if err != nil {
37		return err
38	}
39
40	login, err := input.PromptValue("Avatar URL", "")
41	if err != nil {
42		return err
43	}
44
45	id, err := backend.NewIdentityRaw(name, email, "", login, nil)
46	if err != nil {
47		return err
48	}
49
50	err = id.CommitAsNeeded()
51	if err != nil {
52		return err
53	}
54
55	err = backend.SetUserIdentity(id)
56	if err != nil {
57		return err
58	}
59
60	fmt.Println()
61	fmt.Println(id.Id())
62
63	return nil
64}
65
66var userCreateCmd = &cobra.Command{
67	Use:     "create",
68	Short:   "Create a new identity",
69	PreRunE: loadRepo,
70	RunE:    runUserCreate,
71}
72
73func init() {
74	userCmd.AddCommand(userCreateCmd)
75	userCreateCmd.Flags().SortFlags = false
76}