Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "os" "github.com/c4milo/gofetch" ) func main() { gf := gofetch.New( gofetch.WithDestDir("/tmp"), gofetch.WithConcurrency(100), gofetch.WithETag(), gofetch.WithChecksum("sha256", "29a8b9009509b39d542ecb229787cdf48f05e739a932289de9e9858d7c487c80"), ) progressCh := make(chan gofetch.ProgressReport) doneCh := make(chan struct{}) var myFile *os.File go func() { defer close(doneCh) var err error myFile, err = gf.Fetch( "http://releases.ubuntu.com/16.04.1/ubuntu-16.04.1-server-amd64.iso", progressCh) if err != nil { panic(err) } }() // pogressCh is close by gofetch once a download finishes var totalWritten int64 for p := range progressCh { // p.WrittenBytes does not accumulate, it represents the chunk size written // in the current operation. totalWritten += p.WrittenBytes fmt.Printf("\r%d of %d bytes", totalWritten, p.Total) } <-doneCh fmt.Printf("\nFile saved at %q\n", myFile.Name()) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Fetcher ¶
type Fetcher struct {
// contains filtered or unexported fields
}
Fetcher represents an instance of gofetch, holding global configuration options.
type Option ¶
type Option func(*Fetcher)
Option as explained in http://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html
func WithChecksum ¶
WithChecksum verifies the file once it is fully downloaded using the provided hash and expected value.
func WithConcurrency ¶
WithConcurrency allows you to set the number of goroutines used to download a specific file. By default it is set to 1.
func WithDestDir ¶
WithDestDir allows you to set the destination directory for the downloaded files. By default it is set to: ./
func WithETag ¶
func WithETag() Option
WithETag enables ETag support, meaning that if an already downloaded file is currently on disk and matches the ETag value returned by the server, it will not be downloaded again. By default it is set to false. Be aware that different servers, serving the same file, are likely to return different ETag values, causing the file to be re-downloaded, even though it might already exist on disk.
func WithHTTPClient ¶
WithHTTPClient allows to provide a custom HTTP client. By default a HTTP client with support for read/write timeouts is used.
type ProgressReport ¶
type ProgressReport struct { // Total length in bytes of the file being downloaded Total int64 // Written bytes to disk on a write by write basis. It does not accumulate. WrittenBytes int64 }
ProgressReport represents the current download progress of a given file