func Write(sourceImage, destinationDevice string, compressed bool) error {
	req, err := http.NewRequest("GET", sourceImage, nil)
	if err != nil {
		return err
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode > 300 {
		if resp.StatusCode == 404 {
			return fmt.Errorf("%s not found", sourceImage)
		}
		return fmt.Errorf("%s", resp.Status)
	}
	var out io.Reader
	fileOut, err := os.OpenFile(destinationDevice, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer fileOut.Close()
	if !compressed {
		out = resp.Body
	} else {
		out, err = findDecompressor(sourceImage, resp.Body)
		if err != nil {
			return err
		}
	}
	log.Infof("Beginning write of image [%s] to disk [%s]", filepath.Base(sourceImage), destinationDevice)
	ticker := time.NewTicker(500 * time.Millisecond)
	counter := &WriteCounter{}
	go func() {
		for ; true; <-ticker.C {
			tickerProgress(counter.Total)
		}
	}()
	if _, err = io.Copy(fileOut, io.TeeReader(out, counter)); err != nil {
		ticker.Stop()
		return err
	}
	count, err := io.Copy(fileOut, out)
	if err != nil {
		ticker.Stop()
		return fmt.Errorf("error writing %d bytes to disk [%s] -> %v", count, destinationDevice, err)
	}
	fmt.Printf("\n")
	ticker.Stop()
	if err := fileOut.Sync(); err != nil {
		return fmt.Errorf("failed to sync the block device")
	}
	if err := unix.IoctlSetInt(int(fileOut.Fd()), unix.BLKRRPART, 0); err != nil {
		return fmt.Errorf("error re-probing the partitions for the specified device: %v", err)
	}
	return nil
}
Write will pull an image and write it to local storage device
with compress set to true it will use gzip compression to expand the data before
writing to an underlying device
package image
import (
	"compress/bzip2"
	"compress/gzip"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"time"
	"github.com/dustin/go-humanize"
	"github.com/klauspost/compress/zstd"
	log "github.com/sirupsen/logrus"
	"github.com/ulikunitz/xz"
	"golang.org/x/sys/unix"
)
type WriteCounter struct {
	Total uint64
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
	n := len(p)
	wc.Total += uint64(n)
	return n, nil
}
func tickerProgress(byteCounter uint64) {
	fmt.Printf("\r%s", strings.Repeat(" ", 35))
	fmt.Printf("\rDownloading... %s complete", humanize.Bytes(byteCounter))
}