Skip to content

Commit 11c3c60

Browse files
authored
Merge pull request #14 from terrastruct/mapfs
tmpfs -> mapfs
2 parents 5ffc52e + 65b61c9 commit 11c3c60

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ own repo from this collection we will. Feel free to open an issue to request.
2727
- [./xexec](#xexec)
2828
- [./xhttp](#xhttp)
2929
- [./xmain](#xmain)
30-
- [./tmpfs](#tmpfs)
31-
32-
30+
- [./mapfs](#mapfs)
3331

3432
godoc is the canonical reference but we've provided this index as the godoc UI is frankly
3533
garbage after the move to pkg.go.dev. It's nowhere near as clear and responsive as the old
@@ -119,6 +117,6 @@ xhttp provides HTTP helpers.
119117

120118
xmain implements helpers for building CLI tools.
121119

122-
### [./tmpfs](./tmpfs)
120+
### [./mapfs](./mapfs)
123121

124-
Package tmpfs takes in an memory description of a filesystem and writes it to a temp directory so that it may be used as an io/fs.FS.
122+
Package mapfs takes in a description of a filesystem as a `map[string]string` and writes it to a temp directory so that it may be used as an io/fs.FS.

tmpfs/tmpfs.go mapfs/mapfs.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Package tmpfs takes in an memory description of a filesystem
1+
// Package mapfs takes in a description of a filesystem as a map[string]string
22
// and writes it to a temp directory so that it may be used as an io/fs.FS.
3-
package tmpfs
3+
package mapfs
44

55
import (
66
"errors"
@@ -15,20 +15,20 @@ type FS struct {
1515
fs.FS
1616
}
1717

18-
func Make(m map[string]string) (*FS, error) {
19-
tempDir, err := os.MkdirTemp("", "tmpfs-*")
18+
func New(m map[string]string) (*FS, error) {
19+
tempDir, err := os.MkdirTemp("", "mapfs-*")
2020
if err != nil {
21-
return nil, fmt.Errorf("failed to create root tmpfs dir: %w", err)
21+
return nil, fmt.Errorf("failed to create root mapfs dir: %w", err)
2222
}
2323
for p, s := range m {
2424
p = path.Join(tempDir, p)
2525
err = os.MkdirAll(path.Dir(p), 0755)
2626
if err != nil {
27-
return nil, fmt.Errorf("failed to create tmpfs dir %q: %w", path.Dir(p), err)
27+
return nil, fmt.Errorf("failed to create mapfs dir %q: %w", path.Dir(p), err)
2828
}
2929
err = os.WriteFile(p, []byte(s), 0644)
3030
if err != nil {
31-
return nil, fmt.Errorf("failed to write tmpfs file %q: %w", p, err)
31+
return nil, fmt.Errorf("failed to write mapfs file %q: %w", p, err)
3232
}
3333
}
3434
return &FS{
@@ -43,7 +43,7 @@ func (fs *FS) Close() error {
4343
return nil
4444
}
4545
if err != nil {
46-
return fmt.Errorf("failed to close tmpfs.FS: %w", err)
46+
return fmt.Errorf("failed to close mapfs.FS: %w", err)
4747
}
4848
return nil
4949
}
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package tmpfs_test
1+
package mapfs_test
22

33
import (
44
"io/fs"
55
"testing"
66

77
"oss.terrastruct.com/util-go/assert"
8-
"oss.terrastruct.com/util-go/tmpfs"
8+
"oss.terrastruct.com/util-go/mapfs"
99
)
1010

11-
func TestMemFS(t *testing.T) {
11+
func TestMapFS(t *testing.T) {
1212
t.Parallel()
1313

1414
m := map[string]string{
@@ -18,21 +18,21 @@ func TestMemFS(t *testing.T) {
1818
"nested/nested/nested/nested": "Yuppie Wannabes",
1919
}
2020

21-
tmpfs, err := tmpfs.Make(m)
21+
mapfs, err := mapfs.New(m)
2222
assert.Success(t, err)
2323
t.Cleanup(func() {
24-
err := tmpfs.Close()
24+
err := mapfs.Close()
2525
assert.Success(t, err)
2626
})
2727

2828
for p, s := range m {
29-
b, err := fs.ReadFile(tmpfs, p)
29+
b, err := fs.ReadFile(mapfs, p)
3030
assert.Success(t, err)
3131
assert.Equal(t, s, string(b))
3232
}
3333

34-
_, err = fs.ReadFile(tmpfs, "../escape")
34+
_, err = fs.ReadFile(mapfs, "../escape")
3535
assert.ErrorString(t, err, "stat ../escape: invalid argument")
36-
_, err = fs.ReadFile(tmpfs, "/root")
36+
_, err = fs.ReadFile(mapfs, "/root")
3737
assert.ErrorString(t, err, "stat /root: invalid argument")
3838
}

0 commit comments

Comments
 (0)