-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (64 loc) · 1.47 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
package main
import (
"flag"
"fmt"
"log"
"strings"
tld "github.com/jpillora/go-tld"
)
var build string
func main() {
var url string
var get string
var v bool
flag.StringVar(&url, "url", "", "Enter the URL to parse. Example:\n"+"https://example.com")
flag.StringVar(&get, "get", "", "Get value needed from the URL. Can be:\n"+
"host, hostWithoutPort, domain, subdomain, tld, port, path, fragment, scheme, registeredDomain, query.<paramName>")
flag.BoolVar(&v, "v", false, "Prints the current build version and exits")
flag.Parse()
if v {
fmt.Println(build)
return
}
log.SetFlags(0)
if len(url) == 0 || len(get) == 0 {
log.Fatal("Both --url and --get flags are required.")
}
p, err := tld.Parse(url)
if err != nil {
log.Fatal("Error parsing url: ", err)
}
if strings.HasPrefix(get, "query.") {
param := strings.Split(get, ".")[1]
val := p.Query().Get(param)
if len(val) == 0 {
log.Fatalf("\"%s\" query param not found in url", param)
}
fmt.Print(val)
return
}
switch get {
case "host":
fmt.Print(p.Host)
case "hostWithoutPort":
fmt.Print(p.Hostname())
case "domain":
fmt.Print(p.Domain)
case "subdomain":
fmt.Print(p.Subdomain)
case "tld":
fmt.Print(p.TLD)
case "port":
fmt.Print(p.Port)
case "path":
fmt.Print(p.Path)
case "fragment":
fmt.Print(p.Fragment)
case "scheme":
fmt.Print(p.Scheme)
case "registeredDomain":
fmt.Printf("%s.%s", p.Domain, p.TLD)
default:
log.Fatal("Invalid --get option")
}
}