You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polyfill.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package polyfill
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/go-git/go-billy/v5"
  6. )
  7. // Polyfill is a helper that implements all missing method from billy.Filesystem.
  8. type Polyfill struct {
  9. billy.Basic
  10. c capabilities
  11. }
  12. type capabilities struct{ tempfile, dir, symlink, chroot bool }
  13. // New creates a new filesystem wrapping up 'fs' the intercepts all the calls
  14. // made and errors if fs doesn't implement any of the billy interfaces.
  15. func New(fs billy.Basic) billy.Filesystem {
  16. if original, ok := fs.(billy.Filesystem); ok {
  17. return original
  18. }
  19. h := &Polyfill{Basic: fs}
  20. _, h.c.tempfile = h.Basic.(billy.TempFile)
  21. _, h.c.dir = h.Basic.(billy.Dir)
  22. _, h.c.symlink = h.Basic.(billy.Symlink)
  23. _, h.c.chroot = h.Basic.(billy.Chroot)
  24. return h
  25. }
  26. func (h *Polyfill) TempFile(dir, prefix string) (billy.File, error) {
  27. if !h.c.tempfile {
  28. return nil, billy.ErrNotSupported
  29. }
  30. return h.Basic.(billy.TempFile).TempFile(dir, prefix)
  31. }
  32. func (h *Polyfill) ReadDir(path string) ([]os.FileInfo, error) {
  33. if !h.c.dir {
  34. return nil, billy.ErrNotSupported
  35. }
  36. return h.Basic.(billy.Dir).ReadDir(path)
  37. }
  38. func (h *Polyfill) MkdirAll(filename string, perm os.FileMode) error {
  39. if !h.c.dir {
  40. return billy.ErrNotSupported
  41. }
  42. return h.Basic.(billy.Dir).MkdirAll(filename, perm)
  43. }
  44. func (h *Polyfill) Symlink(target, link string) error {
  45. if !h.c.symlink {
  46. return billy.ErrNotSupported
  47. }
  48. return h.Basic.(billy.Symlink).Symlink(target, link)
  49. }
  50. func (h *Polyfill) Readlink(link string) (string, error) {
  51. if !h.c.symlink {
  52. return "", billy.ErrNotSupported
  53. }
  54. return h.Basic.(billy.Symlink).Readlink(link)
  55. }
  56. func (h *Polyfill) Lstat(path string) (os.FileInfo, error) {
  57. if !h.c.symlink {
  58. return nil, billy.ErrNotSupported
  59. }
  60. return h.Basic.(billy.Symlink).Lstat(path)
  61. }
  62. func (h *Polyfill) Chroot(path string) (billy.Filesystem, error) {
  63. if !h.c.chroot {
  64. return nil, billy.ErrNotSupported
  65. }
  66. return h.Basic.(billy.Chroot).Chroot(path)
  67. }
  68. func (h *Polyfill) Root() string {
  69. if !h.c.chroot {
  70. return string(filepath.Separator)
  71. }
  72. return h.Basic.(billy.Chroot).Root()
  73. }
  74. func (h *Polyfill) Underlying() billy.Basic {
  75. return h.Basic
  76. }
  77. // Capabilities implements the Capable interface.
  78. func (h *Polyfill) Capabilities() billy.Capability {
  79. return billy.Capabilities(h.Basic)
  80. }