Skip to content

Commit

Permalink
added validations for url and email
Browse files Browse the repository at this point in the history
updated info messages
  • Loading branch information
emir committed Oct 9, 2017
1 parent fd6708f commit 24eafef
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions weekly.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"os"
"net/url"
"regexp"
"github.com/urfave/cli"
"gopkg.in/resty.v1"
"github.com/fatih/color"
Expand All @@ -26,19 +28,40 @@ func main() {
Aliases: []string{"u"},
Usage: "Fully qualified URL you want to share with weekly curators. Ex: https://google.com.tr/",
Action: func(c *cli.Context) error {

_, err := url.ParseRequestURI(c.Args().First())

if err != nil {
color.Red("Error: Invalid URL. Be sure you specified fully qualified URL like " +
"https://google.com.tr")

return nil
}

email := findEnvironmentVariableByKey("EMAIL")

if !validateEmail(email) {
color.Red("Error: Invalid Email address.")

return nil
}

resp, err := resty.R().
SetBody(Link{Url: c.Args().First(), Email: findEnvironmentVariableByKey("EMAIL")}).
SetBody(Link{Url: c.Args().First(), Email: email}).
Post(ENDPOINT)

if err != nil {
color.Red("Something went wrong while sharing URL. " +
"Please try again and be sure you specified fully qualified URL like https://google.com.tr/.")
color.Red("Error: Something went wrong while sharing URL.")
}

if resp.StatusCode() == 201 {
color.Green("Thank you!")
if resp.StatusCode() == 422 {
color.Red("Error: Please check EMAIL(from your env.) and URL you've provided.")
} else if resp.StatusCode() == 500 {
color.Red("Error: API fucked up! Please getting touch with: [email protected]")
}

color.Green("Thank you!")

return nil
},
},
Expand All @@ -51,9 +74,16 @@ func findEnvironmentVariableByKey(key string) string {
value := os.Getenv(key)

if len(value) == 0 {
color.Yellow("Please set 'EMAIL' on your environment, we'll use your name as a sender")
color.Yellow("Next time If you set EMAIL= to your environment, we'll send a gift to you!")

return "[email protected]"
}

return value
}

func validateEmail(email string) bool {
Re := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)

return Re.MatchString(email)
}

0 comments on commit 24eafef

Please sign in to comment.