This is an education project that attempts to reimplement the GNU coreutils in Go. You can find the full manual here:
https://www.gnu.org/software/coreutils/manual/coreutils.html
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
821 B
44 lines
821 B
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)
|
|
}
|
|
}
|
|
|