From 3013e0299a9b58dc4c9bb7d1fbe20829ba69bd15 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 23 Jan 2024 20:13:30 +0200 Subject: [PATCH] added helper admin cmd error message in case the migrations are not initialized yet --- cmd/admin.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index fd76a9bd..22cebb05 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -28,12 +28,10 @@ func NewAdminCommand(app core.App) *cobra.Command { func adminCreateCommand(app core.App) *cobra.Command { command := &cobra.Command{ - Use: "create", - Example: "admin create test@example.com 1234567890", - Short: "Creates a new admin account", - // prevents printing the error log twice - SilenceErrors: true, - SilenceUsage: true, + Use: "create", + Example: "admin create test@example.com 1234567890", + Short: "Creates a new admin account", + SilenceUsage: true, RunE: func(command *cobra.Command, args []string) error { if len(args) != 2 { return errors.New("Missing email and password arguments.") @@ -51,6 +49,10 @@ func adminCreateCommand(app core.App) *cobra.Command { admin.Email = args[0] admin.SetPassword(args[1]) + if !app.Dao().HasTable(admin.TableName()) { + return errors.New("Migration are not initialized yet. Please run 'migrate up' and try again.") + } + if err := app.Dao().SaveAdmin(admin); err != nil { return fmt.Errorf("Failed to create new admin account: %v", err) } @@ -84,6 +86,10 @@ func adminUpdateCommand(app core.App) *cobra.Command { return errors.New("The new password must be at least 8 chars long.") } + if !app.Dao().HasTable((&models.Admin{}).TableName()) { + return errors.New("Migration are not initialized yet. Please run 'migrate up' and try again.") + } + admin, err := app.Dao().FindAdminByEmail(args[0]) if err != nil { return fmt.Errorf("Admin with email %s doesn't exist.", args[0])