parent
80d7f6d1a3
commit
1d87e892a3
@ -0,0 +1,2 @@ |
|||||||
|
curl |
||||||
|
curl.exe |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
|
||||||
|
|
||||||
|
build: |
||||||
|
go build .
|
||||||
@ -0,0 +1,3 @@ |
|||||||
|
module lcthw.dev/go/go-coreutils/curl |
||||||
|
|
||||||
|
go 1.25.3 |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"net/http" |
||||||
|
"flag" |
||||||
|
"log" |
||||||
|
"os" |
||||||
|
"io" |
||||||
|
"path/filepath" |
||||||
|
) |
||||||
|
|
||||||
|
func main() { |
||||||
|
var save bool |
||||||
|
var headers bool |
||||||
|
|
||||||
|
flag.BoolVar(&save, "O", false, "output file to local disk") |
||||||
|
flag.BoolVar(&headers, "I", false, "inspect headers") |
||||||
|
flag.Parse() |
||||||
|
|
||||||
|
if flag.NArg() == 0 { log.Fatal("USAGE: curl [-O] [-I] URL") } |
||||||
|
|
||||||
|
target_url := flag.Args()[0] |
||||||
|
|
||||||
|
resp, err := http.Get(target_url) |
||||||
|
if err != nil { log.Fatal(err) } |
||||||
|
|
||||||
|
if headers { |
||||||
|
for key, values := range resp.Header { |
||||||
|
for _, val := range values { |
||||||
|
fmt.Printf("%s: %s\n", key, val) |
||||||
|
} |
||||||
|
} |
||||||
|
} else if save { |
||||||
|
output := filepath.Base(resp.Request.URL.Path) |
||||||
|
|
||||||
|
out, err := os.Create(output) |
||||||
|
if err != nil { log.Fatal(err) } |
||||||
|
|
||||||
|
io.Copy(out, resp.Body) |
||||||
|
} else {
|
||||||
|
io.Copy(os.Stdout, resp.Body) |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue