Skip to content

Commit

Permalink
Merge pull request #48 from Aijeyomah/chore/validate-email
Browse files Browse the repository at this point in the history
Add check to verify email address provided for snapshot
  • Loading branch information
Aijeyomah authored Oct 11, 2024
2 parents 569ddcf + a617ad4 commit fc28f2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/kanvas-snapshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand All @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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)")
Expand Down
10 changes: 10 additions & 0 deletions internal/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)."},
)
}

0 comments on commit fc28f2e

Please sign in to comment.