コード例 #1
0
ファイル: influx.go プロジェクト: ppalucki/appstress
// 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)
	}
}
コード例 #3
0
ファイル: put.go プロジェクト: dropbox/dbxcli
func put(cmd *cobra.Command, args []string) (err error) {
	if len(args) == 0 || len(args) > 2 {
		return errors.New("`put` requires `src` and/or `dst` arguments")
	}

	src := args[0]

	// Default `dst` to the base segment of the source path; use the second argument if provided.
	dst := "/" + path.Base(src)
	if len(args) == 2 {
		dst, err = validatePath(args[1])
		if err != nil {
			return
		}
	}

	contents, err := os.Open(src)
	defer contents.Close()
	if err != nil {
		return
	}

	contentsInfo, err := contents.Stat()
	if err != nil {
		return
	}

	progressbar := &ioprogress.Reader{
		Reader: contents,
		DrawFunc: ioprogress.DrawTerminalf(os.Stderr, func(progress, total int64) string {
			return fmt.Sprintf("Uploading %s/%s",
				humanize.IBytes(uint64(progress)), humanize.IBytes(uint64(total)))
		}),
		Size: contentsInfo.Size(),
	}

	commitInfo := files.NewCommitInfo(dst)
	commitInfo.Mode.Tag = "overwrite"

	// The Dropbox API only accepts timestamps in UTC with second precision.
	commitInfo.ClientModified = time.Now().UTC().Round(time.Second)

	dbx := files.New(config)
	if contentsInfo.Size() > chunkSize {
		return uploadChunked(dbx, progressbar, commitInfo, contentsInfo.Size())
	}

	if _, err = dbx.Upload(commitInfo, progressbar); err != nil {
		return
	}

	return
}
コード例 #4
0
ファイル: get.go プロジェクト: dropbox/dbxcli
func get(cmd *cobra.Command, args []string) (err error) {
	if len(args) == 0 || len(args) > 2 {
		return errors.New("`get` requires `src` and/or `dst` arguments")
	}

	src, err := validatePath(args[0])
	if err != nil {
		return
	}

	// Default `dst` to the base segment of the source path; use the second argument if provided.
	dst := path.Base(src)
	if len(args) == 2 {
		dst = args[1]
	}
	// If `dst` is a directory, append the source filename.
	if f, err := os.Stat(dst); err == nil && f.IsDir() {
		dst = path.Join(dst, path.Base(src))
	}

	arg := files.NewDownloadArg(src)

	dbx := files.New(config)
	res, contents, err := dbx.Download(arg)
	defer contents.Close()
	if err != nil {
		return
	}

	f, err := os.Create(dst)
	defer f.Close()
	if err != nil {
		return
	}

	progressbar := &ioprogress.Reader{
		Reader: contents,
		DrawFunc: ioprogress.DrawTerminalf(os.Stderr, func(progress, total int64) string {
			return fmt.Sprintf("Downloading %s/%s",
				humanize.IBytes(uint64(progress)), humanize.IBytes(uint64(total)))
		}),
		Size: int64(res.Size),
	}

	if _, err = io.Copy(f, progressbar); err != nil {
		return
	}

	return
}
コード例 #5
0
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)
}
コード例 #6
0
ファイル: installer_go.go プロジェクト: rowhit/otto
func (i *GoInstaller) Install(vsn *version.Version) error {
	// All Go projects use a standard URL format
	url := fmt.Sprintf(
		"https://dl.bintray.com/mitchellh/%s/%s_%s_%s_%s.zip",
		i.Name, i.Name, vsn, runtime.GOOS, runtime.GOARCH)

	// Create the temporary directory where we'll store the data
	td, err := ioutil.TempDir("", "otto")
	if err != nil {
		return err
	}
	defer os.RemoveAll(td)

	// Create the ZIP file
	zipPath := filepath.Join(td, "project.zip")
	f, err := os.Create(zipPath)
	if err != nil {
		return err
	}

	// Download the ZIP
	i.Ui.Header(fmt.Sprintf("Downloading %s v%s...", i.Name, vsn))
	i.Ui.Message("URL: " + url)
	i.Ui.Message("")
	resp, err := cleanhttp.DefaultClient().Get(url)
	if err != nil {
		f.Close()
		return err
	}
	if resp.StatusCode != 200 {
		resp.Body.Close()
		f.Close()
		return fmt.Errorf("Error downloading, status code %d", resp.StatusCode)
	}

	// Build the progress bar for our download
	progressR := &ioprogress.Reader{
		Reader:   resp.Body,
		Size:     resp.ContentLength,
		DrawFunc: ioprogress.DrawTerminalf(os.Stdout, i.progressFormat),
	}

	// Listen for interrupts so we can cancel the download
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt)
	defer signal.Stop(sigCh)

	// Copy the zip data
	errCh := make(chan error, 1)
	go func() {
		_, err := io.Copy(f, progressR)
		errCh <- err
	}()

	// Wait for an interrupt or finish
	select {
	case err = <-errCh:
	case <-sigCh:
		err = fmt.Errorf("interrupted")
	}

	// Finish up
	resp.Body.Close()
	f.Close()
	if err != nil {
		return err
	}

	// Open the zip file
	i.Ui.Header("Unzipping downloaded package...")
	zipR, err := zip.OpenReader(zipPath)
	if err != nil {
		return err
	}
	defer zipR.Close()

	// Clear our install directory
	installDir := filepath.Join(i.Dir, i.Name)
	if err := os.RemoveAll(installDir); err != nil {
		return err
	}
	if err := os.MkdirAll(installDir, 0755); err != nil {
		return err
	}

	// Copy all the files
	for _, f := range zipR.File {
		dst, err := os.OpenFile(
			filepath.Join(installDir, f.Name),
			os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
			f.Mode())
		if err != nil {
			return err
		}

		fr, err := f.Open()
		if err != nil {
			dst.Close()
			return err
		}

		_, err = io.Copy(dst, fr)
		fr.Close()
		dst.Close()
		if err != nil {
			return err
		}
	}

	i.Ui.Header(fmt.Sprintf("[green]%s installed successfully!", i.Name))
	return nil
}
コード例 #7
0
ファイル: cli.go プロジェクト: cloudstrack/atlas-upload-cli
// 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
}
コード例 #8
0
ファイル: installer_vagrant.go プロジェクト: mbrodala/otto
func (i *VagrantInstaller) Install(vsn *version.Version) error {
	// Determine the URL
	var url string
	switch runtime.GOOS {
	case "darwin":
		url = fmt.Sprintf(
			"https://releases.hashicorp.com/vagrant/%s/vagrant_%s.dmg",
			vsn, vsn)
	default:
		return fmt.Errorf(
			"Otto doesn't yet support installing Vagrant automatically\n" +
				"on your OS. Please install Vagrant manually.")
	}

	// Create the temporary directory where we'll store the data
	td, err := ioutil.TempDir("", "otto")
	if err != nil {
		return err
	}
	defer os.RemoveAll(td)

	// Create the file path
	path := filepath.Join(td, "vagrant")
	f, err := os.Create(path)
	if err != nil {
		return err
	}

	// Download the file to a temporary directory
	i.Ui.Header(fmt.Sprintf("Downloading Vagrant v%s...", vsn))
	i.Ui.Message("URL: " + url)
	i.Ui.Message("")
	resp, err := cleanhttp.DefaultClient().Get(url)
	if err != nil {
		f.Close()
		return err
	}
	if resp.StatusCode != 200 {
		resp.Body.Close()
		f.Close()
		return fmt.Errorf("Error downloading, status code %d", resp.StatusCode)
	}

	// Build the progress bar for our download
	progressR := &ioprogress.Reader{
		Reader:   resp.Body,
		Size:     resp.ContentLength,
		DrawFunc: ioprogress.DrawTerminalf(os.Stdout, i.progressFormat),
	}

	// Listen for interrupts so we can cancel the download
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt)
	defer signal.Stop(sigCh)

	// Copy the zip data
	errCh := make(chan error, 1)
	go func() {
		_, err := io.Copy(f, progressR)
		errCh <- err
	}()

	// Wait for an interrupt or finish
	select {
	case err = <-errCh:
	case <-sigCh:
		err = fmt.Errorf("interrupted")
	}

	// Finish up
	resp.Body.Close()
	f.Close()
	if err != nil {
		return err
	}

	// Run the proper installer
	switch runtime.GOOS {
	case "darwin":
		return i.installDarwin(path)
	}

	panic("unknown OS")
}
コード例 #9
0
ファイル: rdf2rdf.go プロジェクト: postfix/rdf2rdf
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))
	}
}