diff --git a/cmd/kanvas-snapshot/main.go b/cmd/kanvas-snapshot/main.go index ed87547d81..5c72ec80ad 100644 --- a/cmd/kanvas-snapshot/main.go +++ b/cmd/kanvas-snapshot/main.go @@ -9,6 +9,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "strings" "time" @@ -31,6 +32,8 @@ var ( designName string ) +var emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`) + var generateKanvasSnapshotCmd = &cobra.Command{ Use: "kanvas", Short: "Generate a Kanvas snapshot using a Helm chart", @@ -54,7 +57,9 @@ var generateKanvasSnapshotCmd = &cobra.Command{ designName = ExtractNameFromURI(chartURI) Log.Warnf("No design name provided. Using extracted name: %s", designName) } - + if email != "" && !isValidEmail(email) { + handleError(errors.ErrInvalidEmailFormat(email)) + } // Create Meshery Snapshot designID, err := CreateMesheryDesign(chartURI, designName, email) if err != nil { @@ -255,6 +260,10 @@ func GenerateSnapshot(designID, chartURI, email, assetLocation string) error { return nil } +func isValidEmail(email string) bool { + return emailRegex.MatchString(email) +} + func main() { generateKanvasSnapshotCmd.Flags().StringVarP(&chartURI, "file", "f", "", "URI to Helm chart (required)") diff --git a/internal/errors/error.go b/internal/errors/error.go index 001a987f7c..19cbba28bb 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -14,6 +14,7 @@ var ( ErrDecodingAPICode = "kanvas-snapshot-905" ErrUnexpectedResponseCodeCode = "kanvas-snapshot-906" ErrRequiredFieldNotProvidedCode = "kanvas-snapshot-907" + ErrInvalidEmailFormatCode = "kanvas-snapshot-908" ) func ErrInvalidChartURI(err error) error { @@ -78,3 +79,12 @@ func ErrRequiredFieldNotProvided(err error, field string) error { []string{"Ensure value for flag \"%s\" is correctly provided."}, ) } + +func ErrInvalidEmailFormat(email string) error { + return errors.New(ErrInvalidEmailFormatCode, errors.Alert, + []string{"Invalid email format provided."}, + []string{fmt.Sprintf("The provided email '%s' is not a valid email format.", email)}, + []string{"The email provided for the Kanvas snapshot request is not in the correct format."}, + []string{"Ensure the email address follows the correct format (e.g., user@example.com)."}, + ) +}