Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user: Make user add idempotent #350

Merged
merged 1 commit into from
May 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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