From d7ac4ca44eca382d52722e1fa2b7950edd389456 Mon Sep 17 00:00:00 2001 From: Filip Bartodziej Date: Sat, 9 Jun 2018 18:22:25 +0200 Subject: [PATCH] cmd: deletefile command - fixes #2286 --- cmd/all/all.go | 1 + cmd/deletefile/deletefile.go | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 cmd/deletefile/deletefile.go diff --git a/cmd/all/all.go b/cmd/all/all.go index be05071bc..cb039d436 100644 --- a/cmd/all/all.go +++ b/cmd/all/all.go @@ -19,6 +19,7 @@ import ( _ "github.com/ncw/rclone/cmd/dbhashsum" _ "github.com/ncw/rclone/cmd/dedupe" _ "github.com/ncw/rclone/cmd/delete" + _ "github.com/ncw/rclone/cmd/deletefile" _ "github.com/ncw/rclone/cmd/genautocomplete" _ "github.com/ncw/rclone/cmd/gendocs" _ "github.com/ncw/rclone/cmd/hashsum" diff --git a/cmd/deletefile/deletefile.go b/cmd/deletefile/deletefile.go new file mode 100644 index 000000000..c7790aafd --- /dev/null +++ b/cmd/deletefile/deletefile.go @@ -0,0 +1,37 @@ +package deletefile + +import ( + "github.com/ncw/rclone/cmd" + "github.com/ncw/rclone/fs/operations" + + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +func init() { + cmd.Root.AddCommand(commandDefintion) +} + +var commandDefintion = &cobra.Command{ + Use: "deletefile remote:path", + Short: `Remove a single file path from remote.`, + Long: ` +Remove a single file path from remote. Unlike ` + "`" + `delete` + "`" + ` it cannot be used to +remove a directory and it doesn't obey include/exclude filters - if the specified file exists, +it will always be removed. +`, + Run: func(command *cobra.Command, args []string) { + cmd.CheckArgs(1, 1, command, args) + fs, fileName := cmd.NewFsFile(args[0]) + cmd.Run(true, false, command, func() error { + if fileName == "" { + return errors.Errorf("%s is a directory or doesn't exist", args[0]) + } + fileObj, err := fs.NewObject(fileName) + if err != nil { + return err + } + return operations.DeleteFile(fileObj) + }) + }, +}