Skip to content

Commit

Permalink
Require recursive flag to delete directories
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 6, 2016
1 parent d8a9719 commit bc5809b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
13 changes: 9 additions & 4 deletions drive/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ import (
type DeleteArgs struct {
Out io.Writer
Id string
Recursive bool
}

func (self *Drive) Delete(args DeleteArgs) (err error) {
f, err := self.service.Files.Get(args.Id).Fields("name").Do()
func (self *Drive) Delete(args DeleteArgs) error {
f, err := self.service.Files.Get(args.Id).Fields("name", "mimeType").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}

if isDir(f) && !args.Recursive {
return fmt.Errorf("'%s' is a directory, use the 'recursive' flag to delete directories", f.Name)
}

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

fmt.Fprintf(args.Out, "Removed file '%s'\n", f.Name)
return
fmt.Fprintf(args.Out, "Deleted '%s'\n", f.Name)
return nil
}
10 changes: 9 additions & 1 deletion gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,19 @@ func main() {
},
},
&cli.Handler{
Pattern: "[global] delete <id>",
Pattern: "[global] delete [options] <id>",
Description: "Delete file or directory",
Callback: deleteHandler,
Flags: cli.Flags{
"global": globalFlags,
"options": []cli.Flag{
cli.BoolFlag{
Name: "recursive",
Patterns: []string{"-r", "--recursive"},
Description: "Delete directory and all it's content",
OmitValue: true,
},
},
},
},
&cli.Handler{
Expand Down
1 change: 1 addition & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func deleteHandler(ctx cli.Context) {
err := newDrive(args).Delete(drive.DeleteArgs{
Out: os.Stdout,
Id: args.String("id"),
Recursive: args.Bool("recursive"),
})
checkErr(err)
}
Expand Down

0 comments on commit bc5809b

Please sign in to comment.