Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
areteadvisors committed Sep 7, 2022
1 parent 9f239de commit ff30e37
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# Pycharms
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Usage: curlRed.exe / curlRed
Example (ipv4): curlRed.exe 1.1.1.1
Example (ipv6): curlRed.exe 2a03:2880:2130:cf05:face:b00c::1.Faceb00c
Example (domain): curlRed.exe google.com
Example (batch mode): 1.1.1.1 2a03:2880:2130:cf05:face:b00c::1.Faceb00c google.com
Example (batch mode): curlRed.exe 1.1.1.1 2a03:2880:2130:cf05:face:b00c::1.Faceb00c google.com
```

Build
Expand Down
55 changes: 55 additions & 0 deletions curlRed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Fetchall fetches URLs in parallel and reports their times and sizes.
package main

import (
"fmt"
"github.com/common-nighthawk/go-figure"
"io"
"net/http"
"os"
"strconv"
"time"
)

func main() {
start := time.Now()
ch := make(chan string)

if len(os.Args[1:]) >= 1 {
for _, url := range os.Args[1:] {
go fetch(url, ch) // start a goroutine
}
for range os.Args[1:] {
fmt.Println(<-ch) // receive from channel ch
}
argCount := strconv.Itoa(len(os.Args[1:]))
fmt.Printf("%.2fs elapsed %s %s", time.Since(start).Seconds(), argCount, "lookup(s)")
} else {
banner := figure.NewColorFigure("curlRED", "graffiti", "red", true)
go fetch("", ch) // start a goroutine
banner.Print()
fmt.Println(<-ch) // receive from channel ch
fmt.Println("\n\nUsage: curlRed.exe / curlRed\nExample (ipv4): curlRed.exe 1.1.1.1\nExample (ipv6): curlRed.exe 2a03:2880:2130:cf05:face:b00c::1.Faceb00c\nExample (domain): curlRed.exe google.com\nExample (batch mode): curlRed.exe 1.1.1.1 2a03:2880:2130:cf05:face:b00c::1.Faceb00c google.com")
}
fmt.Println()

}

func fetch(lookup string, ch chan<- string) {
start := time.Now()
resp, err := http.Get("https://curl.red/" + lookup)
if err != nil {
ch <- fmt.Sprint(err) // send to channel ch
return
}

body, err := io.ReadAll(resp.Body)

resp.Body.Close() // don't leak resources
if err != nil {
ch <- fmt.Sprintf("while reading %s: %v", lookup, err)
return
}
secs := time.Since(start).Seconds()
ch <- fmt.Sprintf("%.2fs %s %s %s %s", secs, "|", lookup, "|", body)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module curlRed

go 1.19

require github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=

0 comments on commit ff30e37

Please sign in to comment.