aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user_create.go
diff options
context:
space:
mode:
Diffstat (limited to 'commands/user_create.go')
-rw-r--r--commands/user_create.go53
1 files changed, 23 insertions, 30 deletions
diff --git a/commands/user_create.go b/commands/user_create.go
index df4aa8e9..3da712f3 100644
--- a/commands/user_create.go
+++ b/commands/user_create.go
@@ -1,24 +1,29 @@
package commands
import (
- "fmt"
- "os"
+ "github.com/spf13/cobra"
- "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
+func newUserCreateCommand() *cobra.Command {
+ env := newEnv()
+
+ cmd := &cobra.Command{
+ Use: "create",
+ Short: "Create a new identity.",
+ PreRunE: loadBackend(env),
+ PostRunE: closeBackend(env),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return runUserCreate(env)
+ },
}
- defer backend.Close()
- interrupt.RegisterCleaner(backend.Close)
- preName, err := backend.GetUserName()
+ return cmd
+}
+
+func runUserCreate(env *Env) error {
+ preName, err := env.backend.GetUserName()
if err != nil {
return err
}
@@ -28,7 +33,7 @@ func runUserCreate(cmd *cobra.Command, args []string) error {
return err
}
- preEmail, err := backend.GetUserEmail()
+ preEmail, err := env.backend.GetUserEmail()
if err != nil {
return err
}
@@ -43,7 +48,7 @@ func runUserCreate(cmd *cobra.Command, args []string) error {
return err
}
- id, err := backend.NewIdentityRaw(name, email, "", avatarURL, nil)
+ id, err := env.backend.NewIdentityRaw(name, email, "", avatarURL, nil)
if err != nil {
return err
}
@@ -53,32 +58,20 @@ func runUserCreate(cmd *cobra.Command, args []string) error {
return err
}
- set, err := backend.IsUserIdentitySet()
+ set, err := env.backend.IsUserIdentitySet()
if err != nil {
return err
}
if !set {
- err = backend.SetUserIdentity(id)
+ err = env.backend.SetUserIdentity(id)
if err != nil {
return err
}
}
- _, _ = fmt.Fprintln(os.Stderr)
- fmt.Println(id.Id())
+ env.err.Println()
+ env.out.Println(id.Id())
return nil
}
-
-var userCreateCmd = &cobra.Command{
- Use: "create",
- Short: "Create a new identity.",
- PreRunE: loadRepo,
- RunE: runUserCreate,
-}
-
-func init() {
- userCmd.AddCommand(userCreateCmd)
- userCreateCmd.Flags().SortFlags = false
-}