added helper admin cmd error message in case the migrations are not initialized yet

This commit is contained in:
Gani Georgiev 2024-01-23 20:13:30 +02:00
parent 6fd2e7ab0f
commit 3013e0299a
1 changed files with 12 additions and 6 deletions

View File

@ -28,12 +28,10 @@ func NewAdminCommand(app core.App) *cobra.Command {
func adminCreateCommand(app core.App) *cobra.Command { func adminCreateCommand(app core.App) *cobra.Command {
command := &cobra.Command{ command := &cobra.Command{
Use: "create", Use: "create",
Example: "admin create test@example.com 1234567890", Example: "admin create test@example.com 1234567890",
Short: "Creates a new admin account", Short: "Creates a new admin account",
// prevents printing the error log twice SilenceUsage: true,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(command *cobra.Command, args []string) error { RunE: func(command *cobra.Command, args []string) error {
if len(args) != 2 { if len(args) != 2 {
return errors.New("Missing email and password arguments.") return errors.New("Missing email and password arguments.")
@ -51,6 +49,10 @@ func adminCreateCommand(app core.App) *cobra.Command {
admin.Email = args[0] admin.Email = args[0]
admin.SetPassword(args[1]) 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 { if err := app.Dao().SaveAdmin(admin); err != nil {
return fmt.Errorf("Failed to create new admin account: %v", err) 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.") 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]) admin, err := app.Dao().FindAdminByEmail(args[0])
if err != nil { if err != nil {
return fmt.Errorf("Admin with email %s doesn't exist.", args[0]) return fmt.Errorf("Admin with email %s doesn't exist.", args[0])