From c649257603dcaff1cc994140ca3b6117b90e3f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Mon, 2 May 2022 21:48:07 +0200 Subject: [PATCH] Add user: Make user add idempotent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #201 Signed-off-by: Lukas Kämmerling --- pkg/cli/user.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/cli/user.go b/pkg/cli/user.go index 7ad4bf87..bfbf4306 100644 --- a/pkg/cli/user.go +++ b/pkg/cli/user.go @@ -4,10 +4,11 @@ import ( "errors" "fmt" + "github.com/usefathom/fathom/pkg/models" + log "github.com/sirupsen/logrus" "github.com/urfave/cli" "github.com/usefathom/fathom/pkg/datastore" - "github.com/usefathom/fathom/pkg/models" ) var userCmd = cli.Command{ @@ -58,20 +59,30 @@ func userAdd(c *cli.Context) error { return errors.New("Invalid arguments: missing password") } - user := models.NewUser(email, password) + _, err := app.database.GetUserByEmail(email) + if err != nil { + if err == datastore.ErrNoResults { + user := models.NewUser(email, password) - // set password manually if --skip-bcrypt was given - // this is used to supply an already encrypted password string - if c.Bool("skip-bcrypt") { - user.Password = password - } + // set password manually if --skip-bcrypt was given + // this is used to supply an already encrypted password string + if c.Bool("skip-bcrypt") { + user.Password = password + } - if err := app.database.SaveUser(&user); err != nil { - return fmt.Errorf("Error creating user: %s", err) - } + if err := app.database.SaveUser(&user); err != nil { + return fmt.Errorf("Error creating user: %s", err) + } + + log.Infof("Created user %s", user.Email) + return nil + } - log.Infof("Created user %s", user.Email) + return err + } + log.Infof("A user with this email %s already exists", email) return nil + } func userDelete(c *cli.Context) error {