Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisonleao committed Mar 2, 2021
1 parent c5a4534 commit 8724b5c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
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
76 changes: 76 additions & 0 deletions cmd/vl/main.go
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)
}

}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/npxbr/verify-links

go 1.15

0 comments on commit 8724b5c

Please sign in to comment.