dir.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package term
  11. import (
  12. "fmt"
  13. "os"
  14. "devt.de/krotik/common/stringutil"
  15. "devt.de/krotik/rufs"
  16. )
  17. /*
  18. cmdCd show or change the current directory.
  19. */
  20. func cmdCd(tt *TreeTerm, arg ...string) (string, error) {
  21. if len(arg) > 0 {
  22. tt.cd = tt.parsePathParam(arg[0])
  23. }
  24. return fmt.Sprint(tt.cd, "\n"), nil
  25. }
  26. /*
  27. cmdDir shows a directory listing.
  28. */
  29. func cmdDir(tt *TreeTerm, arg ...string) (string, error) {
  30. return cmdDirListing(tt, false, false, arg...)
  31. }
  32. /*
  33. cmdChecksum shows a directory listing and the checksums.
  34. */
  35. func cmdChecksum(tt *TreeTerm, arg ...string) (string, error) {
  36. return cmdDirListing(tt, false, true, arg...)
  37. }
  38. /*
  39. cmdTree shows the listing of a directory and its subdirectorie
  40. */
  41. func cmdTree(tt *TreeTerm, arg ...string) (string, error) {
  42. return cmdDirListing(tt, true, false, arg...)
  43. }
  44. /*
  45. cmdDirListing shows a directory listing and optional also its subdirectories.
  46. */
  47. func cmdDirListing(tt *TreeTerm, recursive bool, checksum bool, arg ...string) (string, error) {
  48. var dirs []string
  49. var fis [][]os.FileInfo
  50. var err error
  51. var res, rex string
  52. dir := tt.cd
  53. if len(arg) > 0 {
  54. dir = tt.parsePathParam(arg[0])
  55. if len(arg) > 1 {
  56. rex, err = stringutil.GlobToRegex(arg[1])
  57. }
  58. }
  59. if err == nil {
  60. if dirs, fis, err = tt.tree.Dir(dir, rex, recursive, checksum); err == nil {
  61. res = rufs.DirResultToString(dirs, fis)
  62. }
  63. }
  64. return res, err
  65. }