Skip to content

Commit

Permalink
update to use new custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferl committed Mar 11, 2024
1 parent 4ce19f3 commit df1d750
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions cmd/superuser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,36 @@ func main() {
log.Fatal().Msg("username or email already in-use")
}
if err != nil {
if errors.Is(err, data.ErrNoDocuments) {
email := viper.GetString(SuperEmail)
name := viper.GetString(SuperName)
username := viper.GetString(SuperUsername)

log.Info().
Str("name", name).
Str("username", username).
Str("email", email).
Msg("creating superuser")

user := models.NewUserWithRole(email, username, models.SuperRole)
user.Name = name

err = user.SetPassword(viper.GetString(SuperPassword))
if err != nil {
log.Fatal().Err(err).Msg("failed setting superuser password")
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.NotExist {
email := viper.GetString(SuperEmail)
name := viper.GetString(SuperName)
username := viper.GetString(SuperUsername)

log.Info().
Str("name", name).
Str("username", username).
Str("email", email).
Msg("creating superuser")

user := models.NewUserWithRole(email, username, models.SuperRole)
user.Name = name

err = user.SetPassword(viper.GetString(SuperPassword))
if err != nil {
log.Fatal().Err(err).Msg("failed setting superuser password")
}

_, err = svc.Create(ctx, user)
if err != nil {
log.Fatal().Err(err).Msg("failed creating superuser")
}

log.Info().Msg("done")
return
}

_, err = svc.Create(ctx, user)
if err != nil {
log.Fatal().Err(err).Msg("failed creating superuser")
}

log.Info().Msg("done")
} else {
log.Fatal().Err(err).Msg("failed getting superuser")
}
log.Fatal().Err(err).Msg("failed getting superuser")
}
}

0 comments on commit df1d750

Please sign in to comment.