| 1 |
package polyfill |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
|
| 7 |
"github.com/go-git/go-billy/v5" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Polyfill struct { |
| 12 |
billy.Basic |
| 13 |
c capabilities |
| 14 |
} |
| 15 |
|
| 16 |
type capabilities struct{ tempfile, dir, symlink, chroot, chmod bool } |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func New(fs billy.Basic) billy.Filesystem { |
| 21 |
if original, ok := fs.(billy.Filesystem); ok { |
| 22 |
return original |
| 23 |
} |
| 24 |
|
| 25 |
h := &Polyfill{Basic: fs} |
| 26 |
|
| 27 |
_, h.c.tempfile = h.Basic.(billy.TempFile) |
| 28 |
_, h.c.dir = h.Basic.(billy.Dir) |
| 29 |
_, h.c.symlink = h.Basic.(billy.Symlink) |
| 30 |
_, h.c.chroot = h.Basic.(billy.Chroot) |
| 31 |
_, h.c.chmod = h.Basic.(billy.Chmod) |
| 32 |
return h |
| 33 |
} |
| 34 |
|
| 35 |
func (h *Polyfill) TempFile(dir, prefix string) (billy.File, error) { |
| 36 |
if !h.c.tempfile { |
| 37 |
return nil, billy.ErrNotSupported |
| 38 |
} |
| 39 |
|
| 40 |
return h.Basic.(billy.TempFile).TempFile(dir, prefix) |
| 41 |
} |
| 42 |
|
| 43 |
func (h *Polyfill) ReadDir(path string) ([]os.FileInfo, error) { |
| 44 |
if !h.c.dir { |
| 45 |
return nil, billy.ErrNotSupported |
| 46 |
} |
| 47 |
|
| 48 |
return h.Basic.(billy.Dir).ReadDir(path) |
| 49 |
} |
| 50 |
|
| 51 |
func (h *Polyfill) MkdirAll(filename string, perm os.FileMode) error { |
| 52 |
if !h.c.dir { |
| 53 |
return billy.ErrNotSupported |
| 54 |
} |
| 55 |
|
| 56 |
return h.Basic.(billy.Dir).MkdirAll(filename, perm) |
| 57 |
} |
| 58 |
|
| 59 |
func (h *Polyfill) Symlink(target, link string) error { |
| 60 |
if !h.c.symlink { |
| 61 |
return billy.ErrNotSupported |
| 62 |
} |
| 63 |
|
| 64 |
return h.Basic.(billy.Symlink).Symlink(target, link) |
| 65 |
} |
| 66 |
|
| 67 |
func (h *Polyfill) Readlink(link string) (string, error) { |
| 68 |
if !h.c.symlink { |
| 69 |
return "", billy.ErrNotSupported |
| 70 |
} |
| 71 |
|
| 72 |
return h.Basic.(billy.Symlink).Readlink(link) |
| 73 |
} |
| 74 |
|
| 75 |
func (h *Polyfill) Lstat(path string) (os.FileInfo, error) { |
| 76 |
if !h.c.symlink { |
| 77 |
return nil, billy.ErrNotSupported |
| 78 |
} |
| 79 |
|
| 80 |
return h.Basic.(billy.Symlink).Lstat(path) |
| 81 |
} |
| 82 |
|
| 83 |
func (h *Polyfill) Chroot(path string) (billy.Filesystem, error) { |
| 84 |
if !h.c.chroot { |
| 85 |
return nil, billy.ErrNotSupported |
| 86 |
} |
| 87 |
|
| 88 |
return h.Basic.(billy.Chroot).Chroot(path) |
| 89 |
} |
| 90 |
|
| 91 |
func (h *Polyfill) Chmod(path string, mode os.FileMode) error { |
| 92 |
if !h.c.chmod { |
| 93 |
return billy.ErrNotSupported |
| 94 |
} |
| 95 |
|
| 96 |
return h.Basic.(billy.Chmod).Chmod(path, mode) |
| 97 |
} |
| 98 |
|
| 99 |
func (h *Polyfill) Root() string { |
| 100 |
if !h.c.chroot { |
| 101 |
return string(filepath.Separator) |
| 102 |
} |
| 103 |
|
| 104 |
return h.Basic.(billy.Chroot).Root() |
| 105 |
} |
| 106 |
|
| 107 |
func (h *Polyfill) Underlying() billy.Basic { |
| 108 |
return h.Basic |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func (h *Polyfill) Capabilities() billy.Capability { |
| 113 |
return billy.Capabilities(h.Basic) |
| 114 |
} |