-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweekly.go
89 lines (66 loc) · 1.92 KB
/
weekly.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"os"
"net/url"
"regexp"
"github.com/urfave/cli"
"gopkg.in/resty.v1"
"github.com/fatih/color"
)
const ENDPOINT = "https://weekly-share.istanbulphp.org/api/v1/links"
type Link struct {
Url string `json:"url"`
Email string `json:"email"`
}
func main() {
app := cli.NewApp()
app.Name = "weekly"
app.Usage = "u https://emirkarsiyakali.com/php-ve-visual-debt-goruntu-kirliligi-ecbb5267e412"
app.Version = "0.1.0"
app.Commands = []cli.Command{
{
Name: "url",
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: email}).
Post(ENDPOINT)
if err != nil {
color.Red("Error: Something went wrong while sharing URL.")
}
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
},
},
}
app.Run(os.Args)
}
func findEnvironmentVariableByKey(key string) string {
value := os.Getenv(key)
if len(value) == 0 {
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)
}