command_fs_cd.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package shell
  2. import (
  3. "context"
  4. "io"
  5. )
  6. func init() {
  7. Commands = append(Commands, &commandFsCd{})
  8. }
  9. type commandFsCd struct {
  10. }
  11. func (c *commandFsCd) Name() string {
  12. return "fs.cd"
  13. }
  14. func (c *commandFsCd) Help() string {
  15. return `change directory to http://<filer_server>:<port>/dir/
  16. The full path can be too long to type. For example,
  17. fs.ls http://<filer_server>:<port>/some/path/to/file_name
  18. can be simplified as
  19. fs.cd http://<filer_server>:<port>/some/path
  20. fs.ls to/file_name
  21. `
  22. }
  23. func (c *commandFsCd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. input := findInputDirectory(args)
  25. filerServer, filerPort, path, err := commandEnv.parseUrl(input)
  26. if err != nil {
  27. return err
  28. }
  29. if path == "/" {
  30. commandEnv.option.FilerHost = filerServer
  31. commandEnv.option.FilerPort = filerPort
  32. commandEnv.option.Directory = "/"
  33. return nil
  34. }
  35. ctx := context.Background()
  36. err = commandEnv.checkDirectory(ctx, filerServer, filerPort, path)
  37. if err == nil {
  38. commandEnv.option.FilerHost = filerServer
  39. commandEnv.option.FilerPort = filerPort
  40. commandEnv.option.Directory = path
  41. }
  42. return err
  43. }