示例#1
0
// influxFlush copies reads data from reader into influx given by
// uses store function (line by line)
func feedInflux(srcFilename, dstUrl string) {

	if strings.Contains(dstUrl, srcFilename) {
		fmt.Printf("please specify other destination through influxUrl=%q than src file=%q\n", dstUrl, srcFilename)
		os.Exit(1)
	}

	srcFile, err := os.Open(srcFilename)
	ok(err)
	finfo, err := srcFile.Stat()
	ok(err)
	size := finfo.Size()

	// Create the progress reader
	src := &ioprogress.Reader{
		Reader:   srcFile,
		Size:     size,
		DrawFunc: ioprogress.DrawTerminalf(os.Stderr, ioprogress.DrawTextFormatBar(80)),
	}

	dst := openInflux(dstUrl)
	n, err := io.Copy(dst, src)
	ok(err)
	fmt.Printf("copied %d bytes from %q to %q\n", n, srcFilename, dstUrl)
}
示例#2
0
文件: main.go 项目: tobyjoe/grab
func downloadReleaseAsset(r *github.ReleaseAsset, newName *string, newPath *string) {
	var outDir string
	if newPath == nil {
		outDir = binDir()
	} else {
		outDir = *newPath
	}

	downloadUrlString := *r.BrowserDownloadURL

	log.Infof("Downloading release asset: %s", downloadUrlString)
	log.Infof("Copying to: %s", outDir)

	downloadUrl, err := url.Parse(downloadUrlString)
	if err != nil {
		log.Fatalf("Error: Could not parse URL: %v", err)
	}

	var fileName string
	if newName == nil {
		_, fileName = path.Split(downloadUrl.Path)
	} else {
		fileName = *newName
	}

	outName := outDir + "/" + fileName
	out, err := os.Create(outName)
	if err != nil {
		log.Fatalf("Error: Could not create local file: %v", err)
	}

	defer out.Close()
	resp, err := http.Get(downloadUrlString)
	if err != nil {
		log.Fatalf("Error: Could not get remote file: %v", err)
	}
	defer resp.Body.Close()
	bar := ioprogress.DrawTextFormatBar(20)
	progressFunc := ioprogress.DrawTerminalf(os.Stdout, func(progress, total int64) string {
		return fmt.Sprintf("%s %s %20s", fileName, bar(progress, total), ioprogress.DrawTextFormatBytes(progress, total))
	})

	progress := &ioprogress.Reader{
		Reader:       resp.Body,
		Size:         resp.ContentLength,
		DrawInterval: time.Millisecond,
		DrawFunc:     progressFunc,
	}

	_, err = io.Copy(out, progress)
	if err != nil {
		log.Fatalf("Error: Could not copy local file: %v", err)
	}

	err = os.Chmod(outName, 0755)
	if err != nil {
		log.Fatalf("Error: Could not make %s executable. Try with (sudo)?", outName)
	}
}
func downloadFile(token, fileId, filename string, fileSize int64, prefix string, dryrun bool) {
	fmt.Fprintf(os.Stderr, "%s%s\n", prefix, filename)

	if dryrun {
		return
	}

	if _, err := os.Stat(filename); err == nil {
		fmt.Fprintf(os.Stderr, "%s%s already exists!\n", prefix, filename)
		return
	}

	out, err := os.Create(filename + ".tmp")
	defer out.Close()

	url := basespaceApiUrl + "/files/" + fileId + "/content?access_token="
	resp, err := http.Get(url + token)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error downloading URL: %s\n\n", url)
		os.Exit(1)
	}

	defer resp.Body.Close()

	bar := ioprogress.DrawTextFormatBar(20)
	fmtfunc := func(progress, total int64) string {
		return fmt.Sprintf(
			"%s%s %s",
			prefix,
			bar(progress, total),
			ioprogress.DrawTextFormatBytes(progress, total))
	}

	progressR := &ioprogress.Reader{
		Reader:   resp.Body,
		Size:     fileSize,
		DrawFunc: ioprogress.DrawTerminalf(os.Stderr, fmtfunc),
	}

	n, err := io.Copy(out, progressR)
	if err != nil {
		os.Remove(filename + ".tmp")
		log.Fatal(err, n)
	}

	os.Rename(filename+".tmp", filename)
}