From df1d75006ee895328ade8c183bd4e42e6f123c55 Mon Sep 17 00:00:00 2001 From: alexferl Date: Sun, 10 Mar 2024 22:22:55 -0400 Subject: [PATCH] update to use new custom error --- cmd/superuser/main.go | 55 +++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/cmd/superuser/main.go b/cmd/superuser/main.go index d80321f..0aa99d8 100644 --- a/cmd/superuser/main.go +++ b/cmd/superuser/main.go @@ -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") } }