You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB

4 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandFsRm{})
  11. }
  12. type commandFsRm struct {
  13. }
  14. func (c *commandFsRm) Name() string {
  15. return "fs.rm"
  16. }
  17. func (c *commandFsRm) Help() string {
  18. return `remove a file or a folder, recursively delete all files and folders
  19. fs.rm <entry1>
  20. fs.rm /dir/file_name
  21. fs.rm /dir
  22. `
  23. }
  24. func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  25. if len(args) != 1 {
  26. return fmt.Errorf("need to have arguments")
  27. }
  28. targetPath, err := commandEnv.parseUrl(args[0])
  29. if err != nil {
  30. return err
  31. }
  32. targetDir, targetName := util.FullPath(targetPath).DirAndName()
  33. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  34. request := &filer_pb.DeleteEntryRequest{
  35. Directory: targetDir,
  36. Name: targetName,
  37. IgnoreRecursiveError: true,
  38. IsDeleteData: true,
  39. IsRecursive: true,
  40. IsFromOtherCluster: false,
  41. Signatures: nil,
  42. }
  43. _, err = client.DeleteEntry(context.Background(), request)
  44. if err == nil {
  45. fmt.Fprintf(writer, "remove: %s\n", targetPath)
  46. }
  47. return err
  48. })
  49. }