Compare commits

...

4 Commits
v0.0.2 ... main

Author SHA1 Message Date
jolheiser eefdce7b03 Add cache purge (#7)
continuous-integration/woodpecker the build was successful Details
Reviewed-on: #7
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-10-08 21:26:02 +00:00
jolheiser 99bf189237 Move to woodpecker (#6)
continuous-integration/woodpecker the build was successful Details
Reviewed-on: #6
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-10-08 20:55:54 +00:00
jolheiser 7017051b60 Replace build badge with docs badge (#3)
Reviewed-on: #3
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-05-09 04:06:23 +00:00
jolheiser 94a9e1f48c Add ReadFile, interface guard, and drone CI (#2)
Reviewed-on: #2
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2021-05-09 04:02:16 +00:00
6 changed files with 42 additions and 33 deletions

8
.woodpecker.yml 100644
View File

@ -0,0 +1,8 @@
pipeline:
compliance:
image: golang:1.17
commands:
- go test -race ./...
- go vet ./...
when:
event: [ pull_request ]

View File

@ -1,17 +0,0 @@
GO ?= go
.PHONY: vet
vet:
$(GO) vet ./...
.PHONY: fmt
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(GO) test -race ./...
.PHONY: bench
bench:
$(GO) test -benchmem -bench=.

View File

@ -1,4 +1,7 @@
# Overlay
[![Go Reference](https://pkg.go.dev/badge/go.jolheiser.com/overlay.svg)](https://pkg.go.dev/go.jolheiser.com/overlay)
Overlay File System
Overlay is an easy way to implement a file system in such a way that

View File

@ -1,9 +0,0 @@
go test -benchmem -bench=.
goos: linux
goarch: amd64
pkg: go.jolheiser.com/overlay
cpu: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
BenchmarkCache-8 134959974 9.003 ns/op 0 B/op 0 allocs/op
BenchmarkNoCache-8 897212 1369 ns/op 280 B/op 4 allocs/op
PASS
ok go.jolheiser.com/overlay 3.360s

View File

@ -6,6 +6,10 @@ import (
"path"
)
// Interface guard
// fs.ReadFileFS also fulfills fs.FS
var _ fs.ReadFileFS = (*FS)(nil)
// FS is an overlay File System
type FS struct {
fs fs.FS
@ -14,12 +18,17 @@ type FS struct {
cache map[string]bool
}
// PurgeCache purges the cache
func (f *FS) PurgeCache() {
f.cache = make(map[string]bool)
}
func (f *FS) apn(name string) string {
return path.Join(f.root, name)
}
func (f *FS) exists(name string) bool {
if has, ok := f.cache[name]; ok && f.doCache {
if has, ok := f.cache[name]; ok {
return has
}
_, err := os.Stat(f.apn(name))
@ -39,6 +48,14 @@ func (f *FS) Open(name string) (fs.File, error) {
return f.fs.Open(name)
}
// ReadFile reads a file, preferring disk
func (f *FS) ReadFile(name string) ([]byte, error) {
if f.exists(name) {
return os.ReadFile(f.apn(name))
}
return fs.ReadFile(f.fs, name)
}
// ReadDir reads []fs.DirEntry
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
@ -51,10 +68,9 @@ type Option func(*FS) error
// New returns a new FS
func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
x := &FS{
fs: fs,
root: root,
doCache: true,
cache: make(map[string]bool),
fs: fs,
root: root,
cache: make(map[string]bool),
}
for _, opt := range opts {
@ -66,6 +82,15 @@ func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
return x, nil
}
// MustNew returns New and panics on error
func MustNew(root string, fs fs.FS, opts ...Option) *FS {
f, err := New(root, fs, opts...)
if err != nil {
panic(err)
}
return f
}
// WithSub sets a fs.Sub for an FS
func WithSub(sub string) Option {
return func(x *FS) (err error) {

View File

@ -2,7 +2,6 @@ package overlay
import (
"embed"
"io"
"io/fs"
"os"
"strings"
@ -49,7 +48,7 @@ func TestOverlay(t *testing.T) {
}
defer fi.Close()
contents, err := io.ReadAll(fi)
contents, err := x.ReadFile(tc.File)
if err != nil {
t.Log(err)
t.FailNow()