command_fs_cd.go 867 B

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