fetchup

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 16 Imported by: 15

README

Overview

A lib to fetch the target file from remote. It will auto choose the fastest url to download and decompress the file.

Documentation

Overview

Example (Install_golang_migrate)
os.Setenv("GOBIN", "bin")

err := golang_migrate.Install()
if err != nil {
	panic(err)
}

fmt.Print(shellExec("./bin/migrate -version"))
Output:
4.19.0
Example (Install_golangci_lint)
os.Setenv("GOBIN", "bin")

err := golangci_lint.Install()
if err != nil {
	panic(err)
}

fmt.Print(shellExec("./bin/golangci-lint version --short"))
Output:
2.5.0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheDir

func CacheDir() string

func StripFirstDir

func StripFirstDir(dir string) error

StripFirstDir removes the first dir but keep all its children.

Types

type DefaultTransport

type DefaultTransport struct {
	UA string
}

DefaultTransport is the default http transport for fetchup, it auto handles the gzip and user-agent.

func (*DefaultTransport) RoundTrip

func (t *DefaultTransport) RoundTrip(req *http.Request) (*http.Response, error)

type ErrNoURLs

type ErrNoURLs struct {
	URLs []string
}

func (*ErrNoURLs) Error

func (e *ErrNoURLs) Error() string

type Event

type Event string
const (
	EventDownload   Event = "Download:"
	EventProgress   Event = "Progress:"
	EventUnzip      Event = "Unzip:"
	EventDownloaded Event = "Downloaded:"
)

type Fetchup

type Fetchup struct {
	Ctx context.Context

	// SaveTo is the path to save the file.
	SaveTo string

	// URLs is the list of candidates, the fastest one will be used to download the file.
	URLs []string

	Logger Logger

	// SpeedPacketSize is the size of the packet used to calculate the download speed.
	// The size should be much smaller than the whole file size to download.
	SpeedPacketSize int

	MinReportSpan time.Duration

	HttpClient *http.Client
}

func New

func New(us ...string) *Fetchup

func (*Fetchup) Download

func (fu *Fetchup) Download(u string) error

func (*Fetchup) FastestURL

func (fu *Fetchup) FastestURL() (fastest string)

func (*Fetchup) Fetch

func (fu *Fetchup) Fetch() error

func (*Fetchup) Request

func (fu *Fetchup) Request(u string) (*Response, error)

func (*Fetchup) UnTar

func (fu *Fetchup) UnTar(r io.Reader) error

func (*Fetchup) UnZip

func (fu *Fetchup) UnZip(r io.Reader) error

func (*Fetchup) WithContext added in v0.5.0

func (fu *Fetchup) WithContext(ctx context.Context) *Fetchup

func (*Fetchup) WithLogger added in v0.5.0

func (fu *Fetchup) WithLogger(logger Logger) *Fetchup

func (*Fetchup) WithSaveTo added in v0.4.0

func (fu *Fetchup) WithSaveTo(to string) *Fetchup

type Log

type Log func(msg ...interface{})

Log type for Println

func MultiLogger

func MultiLogger(list ...Logger) Log

MultiLogger is similar to https://golang.org/pkg/io/#MultiWriter

func (Log) Println

func (l Log) Println(msg ...interface{})

Println interface

type Logger

type Logger interface {
	// Same as fmt.Printf
	Println(...interface{})
}

Logger interface

var LoggerQuiet Logger = Log(func(_ ...interface{}) {})

LoggerQuiet does nothing

type Response

type Response struct {
	Req            *http.Request
	ResHeader      http.Header
	ProgressedBody io.Reader
	Close          func()
}

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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