command_fs_cd.go 802 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. path, err := commandEnv.parseUrl(findInputDirectory(args))
  24. if err != nil {
  25. return err
  26. }
  27. if path == "/" {
  28. commandEnv.option.Directory = "/"
  29. return nil
  30. }
  31. err = commandEnv.checkDirectory(path)
  32. if err == nil {
  33. commandEnv.option.Directory = path
  34. }
  35. return err
  36. }