Skip to content

Commit

Permalink
Add progress bar to upload from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 14, 2016
1 parent 453c097 commit ef097ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions drive/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const MaxRateInterval = time.Second * 3

func getProgressReader(r io.Reader, w io.Writer, size int64) io.Reader {
// Don't wrap reader if output is discarded or size is too small
if w == ioutil.Discard || size < 1024 * 1024 {
if w == ioutil.Discard || (size > 0 && size < 1024 * 1024) {
return r
}

Expand Down Expand Up @@ -79,7 +79,12 @@ func (self *Progress) draw(isLast bool) {
self.clear()

// Print progress
fmt.Fprintf(self.Writer, "%s/%s", formatSize(self.progress, false), formatSize(self.Size, false))
fmt.Fprintf(self.Writer, "%s", formatSize(self.progress, false))

// Print total size
if self.Size > 0 {
fmt.Fprintf(self.Writer, "/%s", formatSize(self.Size, false))
}

// Print rate
if self.rate > 0 {
Expand Down
6 changes: 5 additions & 1 deletion drive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type UploadStreamArgs struct {
Mime string
Share bool
ChunkSize int64
Progress io.Writer
}

func (self *Drive) UploadStream(args UploadStreamArgs) (err error) {
Expand All @@ -182,10 +183,13 @@ func (self *Drive) UploadStream(args UploadStreamArgs) (err error) {
// Chunk size option
chunkSize := googleapi.ChunkSize(int(args.ChunkSize))

// Wrap file in progress reader
srcReader := getProgressReader(args.In, args.Progress, 0)

fmt.Fprintf(args.Out, "Uploading %s\n", dstFile.Name)
started := time.Now()

f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size").Media(args.In, chunkSize).Do()
f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size").Media(srcReader, chunkSize).Do()
if err != nil {
return fmt.Errorf("Failed to upload file: %s", err)
}
Expand Down
6 changes: 6 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ func main() {
Description: "Share file",
OmitValue: true,
},
cli.BoolFlag{
Name: "noProgress",
Patterns: []string{"--no-progress"},
Description: "Hide progress",
OmitValue: true,
},
),
},
},
Expand Down
1 change: 1 addition & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func uploadStdinHandler(ctx cli.Context) {
Mime: args.String("mime"),
Share: args.Bool("share"),
ChunkSize: args.Int64("chunksize"),
Progress: progressWriter(args.Bool("noProgress")),
})
checkErr(err)
}
Expand Down

0 comments on commit ef097ce

Please sign in to comment.