Skip to content

Commit 3998c26

Browse files
author
Am Laher
committed
rename to mergefs
1 parent 61b4a29 commit 3998c26

File tree

6 files changed

+47
-30
lines changed

6 files changed

+47
-30
lines changed

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
.DEFAULT_GOAL := help
3+
4+
.PHONY: test
5+
test: ## run tests (using go1.16beta1 for now)
6+
go1.16beta1 test -v -race .
7+
8+
.PHONY: help
9+
help:
10+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Chainfs
1+
# Mergefs
22

3-
A go package which chains together filesystems so that you can combine fs.FS filesystems.
3+
A tiny go package which combines together fs.FS filesystems.
44

5-
`chainfs.FS` keeps looking through a slice of `fs.FS` filesytems in order to find a given file. It returns the first match.
5+
`mergefs.FS` looks through a slice of `fs.FS` filesytems in order to find a given file. It returns the first match, or `os.ErrNotExist`.
66

77
[![Go Reference](https://pkg.go.dev/badge/github.com/laher/chainfs.svg)](https://pkg.go.dev/github.com/laher/chainfs)

chainfs.go

-21
This file was deleted.

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/laher/chainfs
1+
module github.com/laher/mergefs
22

3-
go 1.15
3+
go 1.16

mergefs.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package mergefs
2+
3+
import (
4+
"io/fs"
5+
"os"
6+
)
7+
8+
// Merge filesystems
9+
func Merge(filesystems ...fs.FS) fs.FS {
10+
return MergedFS{filesystems: filesystems}
11+
}
12+
13+
// MergedFS combines filesystems. Each filesystem can serve different paths. The first FS takes precedence
14+
type MergedFS struct {
15+
filesystems []fs.FS
16+
}
17+
18+
// Open opens the named file.
19+
func (mfs MergedFS) Open(name string) (fs.File, error) {
20+
for _, fs := range mfs.filesystems {
21+
file, err := fs.Open(name)
22+
if err == nil { // TODO should we return early when it's not an os.ErrNotExist? Should we offer options to decide this behaviour?
23+
return file, nil
24+
}
25+
}
26+
return nil, os.ErrNotExist
27+
}

chainfs_test.go mergefs_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package chainfs
1+
package mergefs_test
22

33
import (
4-
"io/fs"
54
"testing"
65
"testing/fstest"
6+
7+
"github.com/laher/mergefs"
78
)
89

9-
func TestChainFS(t *testing.T) {
10+
func TestMergeFS(t *testing.T) {
1011
a := fstest.MapFS{"a": &fstest.MapFile{Data: []byte("text")}}
1112
b := fstest.MapFS{"b": &fstest.MapFile{Data: []byte("text")}}
12-
fs := FS{Filesystems: []fs.FS{a, b}}
13+
fs := mergefs.Merge(a, b)
1314

1415
if _, err := fs.Open("a"); err != nil {
1516
t.Fatalf("file should exist")

0 commit comments

Comments
 (0)