-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated info messages
- Loading branch information
Showing
1 changed file
with
36 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package main | |
|
||
import ( | ||
"os" | ||
"net/url" | ||
"regexp" | ||
"github.com/urfave/cli" | ||
"gopkg.in/resty.v1" | ||
"github.com/fatih/color" | ||
|
@@ -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 | ||
}, | ||
}, | ||
|
@@ -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) | ||
} |