From 134d32a39f92fc541fd436a7c635ea095897ec8e Mon Sep 17 00:00:00 2001 From: Abhishek Shroff Date: Mon, 26 May 2025 10:03:38 +0530 Subject: [PATCH] [server] Allow not sending email for invite and password reset --- server/internal/command/user/invite.go | 12 +++++++----- server/internal/command/user/password_reset.go | 14 +++++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/server/internal/command/user/invite.go b/server/internal/command/user/invite.go index 476241fd..b8369593 100644 --- a/server/internal/command/user/invite.go +++ b/server/internal/command/user/invite.go @@ -2,6 +2,7 @@ package user import ( "context" + "errors" "fmt" "os" "strings" @@ -72,21 +73,22 @@ func setupInviteCommand() *cobra.Command { } } + if b, _ := cmd.Flags().GetBool("no-email"); !b { + if err := mail.SendWelcomeEmail(u); err != nil { + return errors.New("unable to send welcome email: " + err.Error()) + } + } return nil }) if err != nil { fmt.Println("unable to create user: " + err.Error()) os.Exit(1) } - fmt.Println("User created") - if mail.SendWelcomeEmail(u) != nil { - fmt.Println("unable to send email: " + err.Error()) - os.Exit(1) - } }, } cmd.Flags().StringP("name", "n", "", "Full Name") cmd.Flags().StringP("base-dir", "b", "/home", "Base directory for home") cmd.Flags().BoolP("no-create-home", "M", false, "Do not make home directory") + cmd.Flags().Bool("no-email", false, "Do not send email") return cmd } diff --git a/server/internal/command/user/password_reset.go b/server/internal/command/user/password_reset.go index 034c1756..ce67fa1d 100644 --- a/server/internal/command/user/password_reset.go +++ b/server/internal/command/user/password_reset.go @@ -25,11 +25,19 @@ func setupPasswordResetCommand() *cobra.Command { } else if token, err := manager.CreateResetToken(user); err != nil { fmt.Println("unable to create reset token: " + err.Error()) os.Exit(1) - } else if err := mail.SendPasswordResetEmail(user, token); err != nil { - fmt.Println("unable to send email: " + err.Error()) - os.Exit(1) + } else { + if b, _ := cmd.Flags().GetBool("no-email"); b { + fmt.Println("Created password reset token: " + token) + } else { + if err := mail.SendPasswordResetEmail(user, token); err != nil { + fmt.Println("unable to send email: " + err.Error()) + fmt.Println("Use --no-email print the reset token instead instad of sending it by email") + os.Exit(1) + } + } } }, } + cmd.Flags().Bool("no-email", false, "Do not send email") return cmd }