Skip to content

Commit ae9451a

Browse files
author
Am Laher
committed
super basic chainfs
0 parents  commit ae9451a

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Amir Laher
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Chainfs
2+
3+
A go package which chains together filesystems so that you can combine fs.FS filesystems.
4+
5+
`chainfs.FS` keeps looking through a slice of `fs.FS` filesytems in order to find a given file. It returns the first match.

chainfs.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chainfs
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
)
7+
8+
type FS struct {
9+
Filesystems []fs.FS
10+
}
11+
12+
// Open opens the named file.
13+
func (cfs FS) Open(name string) (fs.File, error) {
14+
for _, fs := range cfs.Filesystems {
15+
file, err := fs.Open(name)
16+
if err == nil {
17+
return file, nil
18+
}
19+
}
20+
return nil, os.ErrNotExist
21+
}

chainfs_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chainfs
2+
3+
import (
4+
"io/fs"
5+
"testing"
6+
"testing/fstest"
7+
)
8+
9+
func TestChainFS(t *testing.T) {
10+
a := fstest.MapFS{"a": &fstest.MapFile{Data: []byte("text")}}
11+
b := fstest.MapFS{"b": &fstest.MapFile{Data: []byte("text")}}
12+
fs := FS{Filesystems: []fs.FS{a, b}}
13+
14+
if _, err := fs.Open("a"); err != nil {
15+
t.Fatalf("file should exist")
16+
}
17+
if _, err := fs.Open("b"); err != nil {
18+
t.Fatalf("file should exist")
19+
}
20+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/laher/chainfs
2+
3+
go 1.15

0 commit comments

Comments
 (0)