mount.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "bytes"
  13. "fmt"
  14. )
  15. /*
  16. cmdReset removes all present mount points or branches.
  17. */
  18. func cmdReset(tt *TreeTerm, arg ...string) (string, error) {
  19. if len(arg) > 0 {
  20. if arg[0] == "mounts" {
  21. tt.tree.Reset(false)
  22. return "Resetting all mounts\n", nil
  23. } else if arg[0] == "branches" {
  24. tt.tree.Reset(true)
  25. return "Resetting all branches and mounts\n", nil
  26. }
  27. }
  28. return "", fmt.Errorf("Can either reset all [mounts] or all [branches] which includes all mount points")
  29. }
  30. /*
  31. cmdBranch lists all known branches or adds a new branch to the tree.
  32. */
  33. func cmdBranch(tt *TreeTerm, arg ...string) (string, error) {
  34. var err error
  35. var res bytes.Buffer
  36. writeKnownBranches := func() {
  37. braches, fps := tt.tree.ActiveBranches()
  38. for i, b := range braches {
  39. res.WriteString(fmt.Sprintf("%v [%v]\n", b, fps[i]))
  40. }
  41. }
  42. if len(arg) == 0 {
  43. writeKnownBranches()
  44. } else if len(arg) > 1 {
  45. var fp = ""
  46. branchName := arg[0]
  47. branchRPC := arg[1]
  48. if len(arg) > 2 {
  49. fp = arg[2]
  50. }
  51. err = tt.tree.AddBranch(branchName, branchRPC, fp)
  52. writeKnownBranches()
  53. } else {
  54. err = fmt.Errorf("branch requires either no or at least 2 parameters")
  55. }
  56. return res.String(), err
  57. }
  58. /*
  59. cmdMount lists all mount points or adds a new mount point to the tree.
  60. */
  61. func cmdMount(tt *TreeTerm, arg ...string) (string, error) {
  62. var err error
  63. var res bytes.Buffer
  64. if len(arg) == 0 {
  65. res.WriteString(tt.tree.String())
  66. } else if len(arg) > 1 {
  67. dir := arg[0]
  68. branchName := arg[1]
  69. writable := !(len(arg) > 2 && arg[2] == "ro") // Writeable unless stated otherwise
  70. if err = tt.tree.AddMapping(dir, branchName, writable); err == nil {
  71. res.WriteString(tt.tree.String())
  72. }
  73. } else {
  74. err = fmt.Errorf("mount requires either 2 or no parameters")
  75. }
  76. return res.String(), err
  77. }