net

package
v0.0.0-...-e7133ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2016 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WgetCommand = cli.Command{
	Name:      "wget",
	Usage:     "retrieve remote documents via HTTP",
	Category:  "net",
	ArgsUsage: "URI",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "method",
			Value: "GET",
			Usage: "HTTP method",
		},
		cli.StringFlag{
			Name:  "O",
			Usage: "output file ('-' for STDIN)",
		},
		cli.BoolFlag{
			Name:  "quiet, q",
			Usage: "supress output",
		},
	},
	Action: func(c *cli.Context) {

		if c.NArg() != 1 {
			util.AbortWithError(errors.New("single argument expected"))
		}
		rawUri := c.Args()[0]

		outFilename := c.String("O")
		if outFilename == "" {
			u, err := url.Parse(rawUri)
			if err != nil {
				util.AbortWithError(err)
			}
			if u.Path == "" {
				outFilename = "index.html"
			} else {
				outFilename = path.Base(u.Path)
			}
		}
		out, err := util.OpenOutput(outFilename)
		if err != nil {
			util.AbortWithError(err)
		}

		req, err := http.NewRequest(c.String("method"), rawUri, nil)
		if err != nil {
			util.AbortWithError(err)
		}

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			util.AbortWithError(err)
		}
		defer resp.Body.Close()

		if resp.StatusCode >= 400 {
			util.AbortWithError(errors.New(resp.Status))
		}

		in := io.Reader(resp.Body)

		if !c.Bool("quiet") {
			bar := pb.New64(resp.ContentLength).SetUnits(pb.U_BYTES)
			bar.Output = os.Stderr
			bar.SetRefreshRate(500 * time.Millisecond)
			bar.ShowSpeed = true
			bar.Start()
			in = bar.NewProxyReader(in)
		}

		_, err = io.Copy(out, in)
		if err != nil {
			util.AbortWithError(err)
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL