-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5a4534
commit 8724b5c
Showing
3 changed files
with
91 additions
and
0 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 |
---|---|---|
@@ -1,2 +1,14 @@ | ||
# verify-links | ||
|
||
CLI tool that helps verify current status of URIs in text files | ||
|
||
www.whatever.com | ||
https://github.com/geongeorge/i-hate-regex | ||
https://www.facebook.com/ | ||
https://www.google.com/ | ||
https://xkcd.com/2293/ | ||
https://this-shouldn't.match@example.com | ||
http://www.example.com/ | ||
https://httpbin.org/status/408 | ||
https://desciclopedia.org/wiki/Zinedine_Zidane | ||
https://deelay.me/10000/www.google.com |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ( | ||
urlRE = regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)`) | ||
skipStatus = flag.String("a", "", "-a 500,400") | ||
timeout = flag.Duration("t", 3*time.Second, "-t 10") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
args := flag.Args() | ||
if len(args) == 0 { | ||
log.Fatal("filename is required") | ||
} | ||
|
||
// read file | ||
file, err := ioutil.ReadFile(args[0]) | ||
if err != nil { | ||
log.Fatalf("error on reading file: %v", err) | ||
} | ||
|
||
// validate skipStatus | ||
var skipped []int | ||
if len(*skipStatus) > 0 { | ||
splitted := strings.Split(*skipStatus, ",") | ||
for _, item := range splitted { | ||
val, err := strconv.Atoi(item) | ||
if err != nil { | ||
log.Fatalf("could not parse skip status value: %v \n", err) | ||
} | ||
skipped = append(skipped, val) | ||
} | ||
} | ||
|
||
isIn := func(val int, items []int) bool { | ||
for _, i := range items { | ||
if i == val { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
matches := urlRE.FindAllString(string(file), -1) | ||
client := &http.Client{ | ||
Timeout: *timeout, | ||
} | ||
|
||
for _, url := range matches { | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
fmt.Printf("Error on getting url %s: %v \n", url, err) | ||
continue | ||
} | ||
|
||
if len(skipped) > 0 && isIn(resp.StatusCode, skipped) { | ||
continue | ||
} | ||
|
||
fmt.Printf("url=%s status=%d \n", url, resp.StatusCode) | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/npxbr/verify-links | ||
|
||
go 1.15 |