Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to verify email address provided for snapshot #48

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)."},
)
}
Loading