-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (82 loc) · 2.16 KB
/
main.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
90
91
92
93
94
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
)
var myClient = &http.Client{Timeout: 10 * time.Second}
func debug(data []byte, err error) {
if err == nil {
fmt.Fprintf(os.Stderr, "%s\n\n", data)
} else {
fmt.Fprintf(os.Stderr, "%s\n\n", err)
}
}
func getJSON(url string, target interface{}, verbose bool) error {
response, err := myClient.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
if verbose {
debug((httputil.DumpResponse(response, true)))
}
return json.NewDecoder(response.Body).Decode(target)
}
func buildPageSpeedURL(targetURL string, strategy string) string {
pageSpeedURL := &url.URL{
Host: "www.googleapis.com",
Scheme: "https",
Path: "pagespeedonline/v4/runPagespeed",
}
q := pageSpeedURL.Query()
q.Set("url", targetURL)
q.Set("strategy", strategy)
pageSpeedURL.RawQuery = q.Encode()
return pageSpeedURL.String()
}
func getPageSpeedScore(targetURL string, strategy string, verbose bool) int {
type PageSpeedResult struct {
RuleGroups struct {
Speed struct {
Score int
}
}
}
pageSpeedURL := buildPageSpeedURL(targetURL, strategy)
result := &PageSpeedResult{}
err := getJSON(pageSpeedURL, result, verbose)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n\n", err)
os.Exit(1)
}
return result.RuleGroups.Speed.Score
}
func getIcon(score, threshold int) string {
if score >= threshold {
return "✅"
}
return "❌"
}
func main() {
verbose := flag.Bool("v", false, "verbose output")
mobileThreshold := flag.Int("mobile", 0, "mobile threshold")
desktopThreshold := flag.Int("desktop", 0, "desktop threshold")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of gps:\n")
fmt.Fprintf(os.Stderr, "gps [flags...] <url>\n")
flag.PrintDefaults()
}
flag.Parse()
targetURL := flag.Arg(0)
mobileScore := getPageSpeedScore(targetURL, "mobile", *verbose)
desktopScore := getPageSpeedScore(targetURL, "desktop", *verbose)
mobileIcon := getIcon(mobileScore, *mobileThreshold)
desktopIcon := getIcon(desktopScore, *desktopThreshold)
fmt.Printf("📱 %d %s 🖥 %d %s %s", mobileScore, mobileIcon, desktopScore, desktopIcon, targetURL)
}