-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from mcuadros/polyfill
helper: new polyfill helper, and mount and chroot simplification
- Loading branch information
Showing
6 changed files
with
182 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package polyfill | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/src-d/go-billy.v3" | ||
) | ||
|
||
// Polyfill is a helper that implements all missing method from billy.Filesystem. | ||
type Polyfill struct { | ||
billy.Basic | ||
c capabilities | ||
} | ||
|
||
type capabilities struct{ tempfile, dir, symlink, chroot bool } | ||
|
||
// New creates a new filesystem wrapping up 'fs' the intercepts all the calls | ||
// made and errors if fs doesn't implement any of the billy interfaces. | ||
func New(fs billy.Basic) billy.Filesystem { | ||
if original, ok := fs.(billy.Filesystem); ok { | ||
return original | ||
} | ||
|
||
h := &Polyfill{Basic: fs} | ||
|
||
_, h.c.tempfile = h.Basic.(billy.TempFile) | ||
_, h.c.dir = h.Basic.(billy.Dir) | ||
_, h.c.symlink = h.Basic.(billy.Symlink) | ||
_, h.c.chroot = h.Basic.(billy.Chroot) | ||
return h | ||
} | ||
|
||
func (h *Polyfill) TempFile(dir, prefix string) (billy.File, error) { | ||
if !h.c.tempfile { | ||
return nil, billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.TempFile).TempFile(dir, prefix) | ||
} | ||
|
||
func (h *Polyfill) ReadDir(path string) ([]os.FileInfo, error) { | ||
if !h.c.dir { | ||
return nil, billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.Dir).ReadDir(path) | ||
} | ||
|
||
func (h *Polyfill) MkdirAll(filename string, perm os.FileMode) error { | ||
if !h.c.dir { | ||
return billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.Dir).MkdirAll(filename, perm) | ||
} | ||
|
||
func (h *Polyfill) Symlink(target, link string) error { | ||
if !h.c.symlink { | ||
return billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.Symlink).Symlink(target, link) | ||
} | ||
|
||
func (h *Polyfill) Readlink(link string) (string, error) { | ||
if !h.c.symlink { | ||
return "", billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.Symlink).Readlink(link) | ||
} | ||
|
||
func (h *Polyfill) Lstat(path string) (os.FileInfo, error) { | ||
if !h.c.symlink { | ||
return nil, billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.Symlink).Lstat(path) | ||
} | ||
|
||
func (h *Polyfill) Chroot(path string) (billy.Filesystem, error) { | ||
if !h.c.chroot { | ||
return nil, billy.ErrNotSupported | ||
} | ||
|
||
return h.Basic.(billy.Chroot).Chroot(path) | ||
} | ||
|
||
func (h *Polyfill) Root() string { | ||
if !h.c.chroot { | ||
return string(filepath.Separator) | ||
} | ||
|
||
return h.Basic.(billy.Chroot).Root() | ||
} | ||
|
||
func (h *Polyfill) Underlying() billy.Basic { | ||
return h.Basic | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package polyfill | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"gopkg.in/src-d/go-billy.v3" | ||
"gopkg.in/src-d/go-billy.v3/test" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
var _ = Suite(&PolyfillSuite{}) | ||
|
||
type PolyfillSuite struct { | ||
Helper billy.Filesystem | ||
Underlying billy.Filesystem | ||
} | ||
|
||
func (s *PolyfillSuite) SetUpTest(c *C) { | ||
s.Helper = New(&test.BasicMock{}) | ||
} | ||
|
||
func (s *PolyfillSuite) TestTempFile(c *C) { | ||
_, err := s.Helper.TempFile("", "") | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestReadDir(c *C) { | ||
_, err := s.Helper.ReadDir("") | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestMkdirAll(c *C) { | ||
err := s.Helper.MkdirAll("", 0) | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestSymlink(c *C) { | ||
err := s.Helper.Symlink("", "") | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestReadlink(c *C) { | ||
_, err := s.Helper.Readlink("") | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestLstat(c *C) { | ||
_, err := s.Helper.Lstat("") | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestChroot(c *C) { | ||
_, err := s.Helper.Chroot("") | ||
c.Assert(err, Equals, billy.ErrNotSupported) | ||
} | ||
|
||
func (s *PolyfillSuite) TestRoot(c *C) { | ||
c.Assert(s.Helper.Root(), Equals, string(filepath.Separator)) | ||
} |