scripts

misc scripts and tools
git clone git://git.2f30.org/scripts
Log | Files | Refs

get.go (778B)


      1 // Download files over http with go
      2 // Default to stdout unless [-o outfile] has been specified
      3 package main
      4 
      5 import (
      6 	"flag"
      7 	"fmt"
      8 	"io"
      9 	"log"
     10 	"net/http"
     11 	"os"
     12 )
     13 
     14 var (
     15 	outFile = flag.String("o", "", "Output File")
     16 )
     17 
     18 func usage() {
     19 	fmt.Fprintf(os.Stderr, "usage: %s [-o outfile] url\n", os.Args[0])
     20 	os.Exit(2)
     21 }
     22 
     23 func main() {
     24 	log.SetPrefix("get ")
     25 	flag.Usage = usage
     26 	flag.Parse()
     27 
     28 	args := flag.Args()
     29 	if len(args) < 1 {
     30 		usage()
     31 	}
     32 
     33 	var out *os.File
     34 	if *outFile != "" {
     35 		o, err := os.Create(*outFile)
     36 		if err != nil {
     37 			log.Fatal(err)
     38 		}
     39 		defer o.Close()
     40 		out = o
     41 	} else {
     42 		out = os.Stdout
     43 	}
     44 
     45 	r, err := http.Get(args[0])
     46 	if err != nil {
     47 		log.Fatal(err)
     48 	}
     49 	defer r.Body.Close()
     50 	_, err = io.Copy(out, r.Body)
     51 	if err != nil {
     52 		log.Fatal(err)
     53 	}
     54 }