Debian Codegraph @

helper/polyfill/polyfill.go

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