Skip to content

Commit

Permalink
Cursed arg parsing... but now request timeouts are configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
port19x committed Aug 26, 2024
1 parent 832edd2 commit 1949458
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"sync"
"time"
)
Expand All @@ -20,6 +21,7 @@ var allowedStatusCodes = []int{
http.StatusAccepted, // Accepted -> e.g. DuckDuckGo
}
var exitCode int = 0
var timeout int = 5
var mutex sync.Mutex
var strict bool = false

Expand Down Expand Up @@ -57,10 +59,8 @@ func contains(slice []int, item int) bool {
// This checks a list of links. It also expects the corresponding toplevel, to make fixing easier
// This is an outside function to make it easily parallelizable
func isLinkAlive(url string, toplevel string) string {
// If you think this timeout is excessive, go to https://www.gnu.org/software/gawk/manual/gawk.html
// I got 35 seconds with my browser. On the first try!
client := http.Client{
Timeout: time.Minute,
Timeout: time.Duration(timeout) * time.Second,
}
resp, err := client.Get(url)
if err != nil {
Expand All @@ -76,19 +76,37 @@ func isLinkAlive(url string, toplevel string) string {
}
}

// Read man shift. This function kinda reimplements that posix shell builtin
func shift() {
os.Args = os.Args[1:]
}

func main() {
var body string
// I swear, one more argument and I'll use the flag module
if len(os.Args) < 2 || os.Args[1] == "-h" || os.Args[1] == "--help" {
fmt.Println(helptext)
return
}
if os.Args[1] == "-V" || os.Args[1] == "--version" {
fmt.Println("v1.3 - 20240826")
fmt.Println("v1.4 - 20240826")
return
}
// Sorry Dijkstra
harmful:
if os.Args[1] == "-s" || os.Args[1] == "--strict" {
strict = true
body = reqWrap(os.Args[2]) // Assumption: timeouts on the website being deadlink-checked do not occur
shift()
goto harmful
} else if os.Args[1] == "-t" || os.Args[1] == "--timeout" {
var err error
timeout, err = strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalf("timeout needs to be an integer", err)
}
shift()
shift()
goto harmful
} else {
body = reqWrap(os.Args[1]) // Assumption: timeouts on the website being deadlink-checked do not occur
}
Expand Down

0 comments on commit 1949458

Please sign in to comment.