Skip to content

Commit

Permalink
Merge pull request #350 from usefathom/idempotent-users
Browse files Browse the repository at this point in the history
Add user: Make user add idempotent
  • Loading branch information
LKaemmerling authored May 2, 2022
2 parents 7a234a8 + c649257 commit 0a3ffdb
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pkg/cli/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0a3ffdb

Please sign in to comment.