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.

99 lines
2.0 KiB

  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. "strings"
  9. )
  10. func init() {
  11. commands = append(commands, &commandFsCd{})
  12. }
  13. type commandFsCd struct {
  14. }
  15. func (c *commandFsCd) Name() string {
  16. return "fs.cd"
  17. }
  18. func (c *commandFsCd) Help() string {
  19. return `change directory to http://<filer_server>:<port>/dir/
  20. The full path can be too long to type. For example,
  21. fs.ls http://<filer_server>:<port>/some/path/to/file_name
  22. can be simplified as
  23. fs.cd http://<filer_server>:<port>/some/path
  24. fs.ls to/file_name
  25. `
  26. }
  27. func (c *commandFsCd) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
  28. input := ""
  29. if len(args) > 0 {
  30. input = args[len(args)-1]
  31. }
  32. filerServer, filerPort, path, err := commandEnv.parseUrl(input)
  33. if err != nil {
  34. return err
  35. }
  36. dir, name := filer2.FullPath(path).DirAndName()
  37. if strings.HasSuffix(path, "/") {
  38. if path == "/" {
  39. dir, name = "/", ""
  40. } else {
  41. dir, name = filer2.FullPath(path[0:len(path)-1]).DirAndName()
  42. }
  43. }
  44. ctx := context.Background()
  45. err = commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  46. resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  47. Directory: dir,
  48. Prefix: name,
  49. StartFromFileName: name,
  50. InclusiveStartFrom: true,
  51. Limit: 1,
  52. })
  53. if listErr != nil {
  54. return listErr
  55. }
  56. if path == "" || path == "/" {
  57. return nil
  58. }
  59. if len(resp.Entries) == 0 {
  60. return fmt.Errorf("entry not found")
  61. }
  62. if resp.Entries[0].Name != name {
  63. println("path", path, "dir", dir, "name", name, "found", resp.Entries[0].Name)
  64. return fmt.Errorf("not a valid directory, found %s", resp.Entries[0].Name)
  65. }
  66. if !resp.Entries[0].IsDirectory {
  67. return fmt.Errorf("not a directory")
  68. }
  69. return nil
  70. })
  71. if err == nil {
  72. commandEnv.option.FilerHost = filerServer
  73. commandEnv.option.FilerPort = filerPort
  74. commandEnv.option.Directory = path
  75. }
  76. return err
  77. }