feat: Support relative root directories without BasePathFs

This commit is contained in:
Felix Pojtinger
2021-12-19 22:40:52 +01:00
parent ad3471c86a
commit 63469ad8d1
3 changed files with 22 additions and 2 deletions
+13
View File
@@ -4,6 +4,7 @@ import (
"os"
"time"
"github.com/pojntfx/stfs/internal/pathext"
"github.com/pojntfx/stfs/pkg/config"
"github.com/spf13/afero"
)
@@ -17,14 +18,26 @@ func Cache(
) (afero.Fs, error) {
switch cacheType {
case CacheTypeMemory:
if pathext.IsRoot(root) {
return afero.NewCacheOnReadFs(base, afero.NewMemMapFs(), ttl), nil
}
return afero.NewCacheOnReadFs(afero.NewBasePathFs(base, root), afero.NewMemMapFs(), ttl), nil
case CacheTypeDir:
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return nil, err
}
if pathext.IsRoot(root) {
return afero.NewCacheOnReadFs(base, afero.NewBasePathFs(afero.NewOsFs(), cacheDir), ttl), nil
}
return afero.NewCacheOnReadFs(afero.NewBasePathFs(base, root), afero.NewBasePathFs(afero.NewOsFs(), cacheDir), ttl), nil
case config.NoneKey:
if pathext.IsRoot(root) {
return base, nil
}
return afero.NewBasePathFs(base, root), nil
default:
return nil, ErrCacheTypeUnsupported