Esempio n. 1
0
func main() {
	downloadTarget := "http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-amd64.iso"
	resp, err := http.Get(downloadTarget)
	if err != nil {
		log.Fatal(err)
	}

	size := resp.ContentLength
	progressR := &ioprogress.Reader{
		Reader:       resp.Body,
		Size:         size,
		DrawInterval: time.Millisecond,
		DrawFunc: func(progress, total int64) error {
			if progress == total {
				fmt.Printf("\rDownloading: %s%10s", ioprogress.DrawTextFormatBytes(size, size), "")
				return nil
			}
			fmt.Printf("\rDownloading: %s%10s", ioprogress.DrawTextFormatBytes(progress, total), "")
			return nil
		},
	}

	file, err := open("ubuntu.iso")
	if err != nil {
		log.Fatal(err)
	}
	if _, err = io.Copy(file, progressR); err != nil {
		log.Fatal(err)
	}
}
Esempio n. 2
0
File: main.go Progetto: 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)
}
Esempio n. 4
0
func (i *GoInstaller) progressFormat(progress, total int64) string {
	return fmt.Sprintf("    %s", ioprogress.DrawTextFormatBytes(progress, total))
}
Esempio n. 5
0
// Run invokes the CLI with the given arguments. The first argument is always
// the name of the application. This method slices accordingly.
func (cli *CLI) Run(args []string) int {
	// Initialize the logger to start (overridden later if debug is given)
	cli.initLogger(os.Getenv("ATLAS_LOG"))

	var debug, version bool
	var archiveOpts archive.ArchiveOpts
	var uploadOpts UploadOpts

	flags := flag.NewFlagSet(Name, flag.ContinueOnError)
	flags.SetOutput(cli.errStream)
	flags.Usage = func() {
		fmt.Fprintf(cli.errStream, usage, Name)
	}
	flags.BoolVar(&archiveOpts.VCS, "vcs", false,
		"use VCS to detect which files to upload")
	flags.StringVar(&uploadOpts.URL, "address", "",
		"Atlas server address")
	flags.StringVar(&uploadOpts.Token, "token", "",
		"Atlas API token")
	flags.Var((*FlagSliceVar)(&archiveOpts.Exclude), "exclude",
		"files/folders to exclude")
	flags.Var((*FlagSliceVar)(&archiveOpts.Include), "include",
		"files/folders to include")
	flags.Var((*FlagMetadataVar)(&uploadOpts.Metadata), "metadata",
		"arbitrary metadata to pass along with the request")
	flags.BoolVar(&debug, "debug", false,
		"turn on debug output")
	flags.BoolVar(&version, "version", false,
		"display the version")

	// Parse all the flags
	if err := flags.Parse(args[1:]); err != nil {
		return ExitCodeParseFlagsError
	}

	// Turn on debug mode if requested
	if debug {
		levelFilter.SetMinLevel(logutils.LogLevel("DEBUG"))
	}

	// Version
	if version {
		fmt.Fprintf(cli.errStream, "%s v%s\n", Name, Version)
		return ExitCodeOK
	}

	// Get the parsed arguments (the ones left over after all the flags have been
	// parsed)
	parsedArgs := flags.Args()

	if len(parsedArgs) != 2 {
		fmt.Fprintf(cli.errStream, "cli: must specify two arguments - slug, path\n")
		flags.Usage()
		return ExitCodeBadArgs
	}

	// Get the name of the app and the path to archive
	slug, path := parsedArgs[0], parsedArgs[1]
	uploadOpts.Slug = slug

	// Get the archive reader
	r, err := archive.CreateArchive(path, &archiveOpts)
	if err != nil {
		fmt.Fprintf(cli.errStream, "error archiving: %s\n", err)
		return ExitCodeArchiveError
	}
	defer r.Close()

	// Put a progress bar around the reader
	pr := &ioprogress.Reader{
		Reader: r,
		Size:   r.Size,
		DrawFunc: ioprogress.DrawTerminalf(os.Stdout, func(p, t int64) string {
			return fmt.Sprintf(
				"Uploading %s: %s",
				slug,
				ioprogress.DrawTextFormatBytes(p, t))
		}),
	}

	// Start the upload
	doneCh, uploadErrCh, err := Upload(pr, r.Size, &uploadOpts)
	if err != nil {
		fmt.Fprintf(cli.errStream, "error starting upload: %s\n", err)
		return ExitCodeUploadError
	}

	select {
	case err := <-uploadErrCh:
		fmt.Fprintf(cli.errStream, "error uploading: %s\n", err)
		return ExitCodeUploadError
	case version := <-doneCh:
		fmt.Printf("Uploaded %s v%d\n", slug, version)
	}

	return ExitCodeOK
}
Esempio n. 6
0
func main() {
	log.SetFlags(0)
	log.SetPrefix("ERROR: ")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, usage)
	}
	input := flag.String("in", "", "Input file")
	output := flag.String("out", "", "Output file")
	verbose := flag.Bool("v", false, "Verbose mode")
	stream := flag.Bool("stream", true, "Streaming mode")
	flag.Parse()

	if *input == "" || *output == "" {
		fmt.Println("Usage:")
		flag.PrintDefaults()
		os.Exit(1)
	}

	inFile, err := os.Open(*input)
	if err != nil {
		log.Fatal(err)
	}
	defer inFile.Close()

	stat, err := inFile.Stat()
	if err != nil {
		log.Fatal(err)
	}

	var inFileRdr io.Reader
	if *verbose {
		inFileRdr = &ioprogress.Reader{
			Reader:       inFile,
			Size:         stat.Size(),
			DrawInterval: time.Microsecond,
			DrawFunc: ioprogress.DrawTerminalf(os.Stdout, func(p, t int64) string {
				return ioprogress.DrawTextFormatBytes(p, t)
			}),
		}
	} else {
		inFileRdr = inFile
	}

	outFile, err := os.Create(*output)
	if err != nil {
		log.Fatal(err)
	}
	defer outFile.Close()

	inExt := fileExtension(*input)
	outExt := fileExtension(*output)

	if inExt == outExt {
		log.Fatal("No conversion necessary. Input and output formats are identical.")
	}

	var inFormat, outFormat rdf.Format

	switch inExt {
	case "nt":
		inFormat = rdf.NTriples
	case "nq":
		inFormat = rdf.NQuads
	case "ttl":
		inFormat = rdf.Turtle
	case "xml", "rdf", "rdfxml":
		inFormat = rdf.RDFXML
	case "":
		log.Fatal("Unknown file format. No file extension on input file.")
	default:
		log.Fatalf("Unsopported file exension on input file: %s", inFile.Name())
	}

	switch outExt {
	case "nt":
		outFormat = rdf.NTriples
	case "nq":
		// No other quad-formats supported ATM
		log.Fatal("Serializing to N-Quads currently not supported.")
	case "ttl":
		outFormat = rdf.Turtle
	case "":
		log.Fatal("Unknown file format. No file extension on output file.")
	default:
		log.Fatalf("Unsopported file exension on output file: %s", outFile.Name())
	}

	t0 := time.Now()
	n := tripleToTriple(inFileRdr, outFile, inFormat, outFormat, *stream)
	if *verbose {
		fmt.Printf("Done. Converted %d triples in %v.\n", n, time.Now().Sub(t0))
	}
}