Skip to content

Commit

Permalink
Add delete option for upload and downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 16, 2016
1 parent a44ec78 commit 5d56138
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions drive/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ func (self *Drive) Delete(args DeleteArgs) error {
fmt.Fprintf(args.Out, "Deleted '%s'\n", f.Name)
return nil
}

func (self *Drive) deleteFile(fileId string) error {
err := self.service.Files.Delete(fileId).Do()
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
return nil
}
12 changes: 12 additions & 0 deletions drive/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DownloadArgs struct {
Path string
Force bool
Recursive bool
Delete bool
Stdout bool
}

Expand All @@ -43,6 +44,17 @@ func (self *Drive) Download(args DownloadArgs) error {
if !args.Stdout {
fmt.Fprintf(args.Out, "Downloaded %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(bytes, false))
}

if args.Delete {
err = self.deleteFile(args.Id)
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}

if !args.Stdout {
fmt.Fprintf(args.Out, "Removed %s\n", args.Id)
}
}
return err
}

Expand Down
10 changes: 10 additions & 0 deletions drive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type UploadArgs struct {
Mime string
Recursive bool
Share bool
Delete bool
ChunkSize int64
}

Expand Down Expand Up @@ -52,6 +53,15 @@ func (self *Drive) Upload(args UploadArgs) error {

fmt.Fprintf(args.Out, "File is readable by anyone at %s\n", f.WebContentLink)
}

if args.Delete {
err = os.Remove(args.Path)
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
fmt.Fprintf(args.Out, "Removed %s\n", args.Path)
}

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func main() {
Patterns: []string{"--path"},
Description: "Download path",
},
cli.BoolFlag{
Name: "delete",
Patterns: []string{"--delete"},
Description: "Delete remote file when download is successful",
OmitValue: true,
},
cli.BoolFlag{
Name: "noProgress",
Patterns: []string{"--no-progress"},
Expand Down Expand Up @@ -155,6 +161,12 @@ func main() {
Description: "Share file",
OmitValue: true,
},
cli.BoolFlag{
Name: "delete",
Patterns: []string{"--delete"},
Description: "Delete local file when upload is successful",
OmitValue: true,
},
cli.IntFlag{
Name: "chunksize",
Patterns: []string{"--chunksize"},
Expand Down
20 changes: 20 additions & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ func listChangesHandler(ctx cli.Context) {

func downloadHandler(ctx cli.Context) {
args := ctx.Args()
checkDownloadArgs(args)
err := newDrive(args).Download(drive.DownloadArgs{
Out: os.Stdout,
Id: args.String("fileId"),
Force: args.Bool("force"),
Path: args.String("path"),
Delete: args.Bool("delete"),
Recursive: args.Bool("recursive"),
Stdout: args.Bool("stdout"),
Progress: progressWriter(args.Bool("noProgress")),
Expand Down Expand Up @@ -90,6 +92,7 @@ func downloadRevisionHandler(ctx cli.Context) {

func uploadHandler(ctx cli.Context) {
args := ctx.Args()
checkUploadArgs(args)
err := newDrive(args).Upload(drive.UploadArgs{
Out: os.Stdout,
Progress: progressWriter(args.Bool("noProgress")),
Expand All @@ -99,6 +102,7 @@ func uploadHandler(ctx cli.Context) {
Mime: args.String("mime"),
Recursive: args.Bool("recursive"),
Share: args.Bool("share"),
Delete: args.Bool("delete"),
ChunkSize: args.Int64("chunksize"),
})
checkErr(err)
Expand Down Expand Up @@ -366,3 +370,19 @@ func conflictResolution(args cli.Arguments) drive.ConflictResolution {

return drive.NoResolution
}

func checkUploadArgs(args cli.Arguments) {
if args.Bool("recursive") && args.Bool("delete") {
ExitF("--delete is not allowed for recursive uploads")
}

if args.Bool("recursive") && args.Bool("share") {
ExitF("--share is not allowed for recursive uploads")
}
}

func checkDownloadArgs(args cli.Arguments) {
if args.Bool("recursive") && args.Bool("delete") {
ExitF("--delete is not allowed for recursive downloads")
}
}

0 comments on commit 5d56138

Please sign in to comment.