-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (60 loc) · 1.17 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
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"sort"
"github.com/jackdanger/collectlinks"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) > 0 {
urlStr := args[0]
u, err := url.Parse(urlStr)
if err != nil {
fmt.Printf("Invalid URL: %s\n", err)
os.Exit(1)
}
if resp, err := http.Get(urlStr); err != nil {
fmt.Printf("Getting URL failed: %s\n", err)
os.Exit(1)
} else {
links := collectlinks.All(resp.Body)
for i, l := range links {
if x, err := url.Parse(l); err == nil && x.Host != "" {
// No changes needed
} else {
// Probably not a full URL
lu := *u
lu.Path = l
links[i] = lu.String()
}
}
// De-duplicate
links = distinct(links)
// Sort links
sort.Strings(links)
// Show results
for _, l := range links {
fmt.Println(l)
}
}
} else {
fmt.Println("Please provide URL argument")
os.Exit(1)
}
}
func distinct(arg []string) []string {
tempMap := make(map[string]struct{})
for _, s := range arg {
tempMap[s] = struct{}{}
}
result := make([]string, 0, len(tempMap))
for key := range tempMap {
result = append(result, key)
}
return result
}