From 21beb15728e3c37f4fdbb9ecd648505fb657e4a8 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Fri, 10 Jan 2025 10:22:46 +0200 Subject: [PATCH] Enable the `iofs` adapter to also return other interfaces from `io/fs` Instead of relying on difficult casting, this improves developer experience by providing easy ways of convering a billy filesystem into the rest of the filesystem interfaces that are available in `io/fs`. This is a safe operation since we already ensure the type implements such interfaces some lines below my changes. Signed-off-by: Juan Antonio Osorio --- helper/iofs/iofs.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/helper/iofs/iofs.go b/helper/iofs/iofs.go index be8f348..cde9ad7 100644 --- a/helper/iofs/iofs.go +++ b/helper/iofs/iofs.go @@ -16,6 +16,21 @@ func New(fs billyfs.Basic) fs.FS { return &adapterFs{fs: polyfill.New(fs)} } +// NewStatFS adapts a billy.Filesystem to a io.fs.StatFS. +func NewStatFS(fs billyfs.Basic) fs.StatFS { + return &adapterFs{fs: polyfill.New(fs)} +} + +// NewReadDirFS adapts a billy.Filesystem to a io.fs.ReadDirFS. +func NewReadDirFS(fs billyfs.Basic) fs.ReadDirFS { + return &adapterFs{fs: polyfill.New(fs)} +} + +// NewReadFileFS adapts a billy.Filesystem to a io.fs.ReadFileFS. +func NewReadFileFS(fs billyfs.Basic) fs.ReadFileFS { + return &adapterFs{fs: polyfill.New(fs)} +} + type adapterFs struct { fs billyfs.Filesystem }