Add ReadFile, interface guard, and drone CI #2
|
@ -0,0 +1,17 @@
|
||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
name: compliance
|
||||||
|
trigger:
|
||||||
|
event:
|
||||||
|
- pull_request
|
||||||
|
steps:
|
||||||
|
- name: build
|
||||||
|
pull: always
|
||||||
|
image: golang:1.16
|
||||||
|
commands:
|
||||||
|
- make test
|
||||||
|
- name: check
|
||||||
|
pull: always
|
||||||
|
image: golang:1.16
|
||||||
|
commands:
|
||||||
|
- make vet
|
|
@ -1,4 +1,7 @@
|
||||||
# Overlay
|
# Overlay
|
||||||
|
|
||||||
|
[![Build Status](https://ci.jojodev.com/api/badges/jolheiser/overlay/status.svg?ref=refs/heads/main)](https://ci.jojodev.com/jolheiser/overlay)
|
||||||
|
|
||||||
Overlay File System
|
Overlay File System
|
||||||
|
|
||||||
Overlay is an easy way to implement a file system in such a way that
|
Overlay is an easy way to implement a file system in such a way that
|
||||||
|
|
21
overlay.go
21
overlay.go
|
@ -6,6 +6,10 @@ import (
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Interface guard
|
||||||
|
// fs.ReadFileFS also fulfills fs.FS
|
||||||
|
var _ fs.ReadFileFS = (*FS)(nil)
|
||||||
|
|
||||||
// FS is an overlay File System
|
// FS is an overlay File System
|
||||||
type FS struct {
|
type FS struct {
|
||||||
fs fs.FS
|
fs fs.FS
|
||||||
|
@ -39,6 +43,14 @@ func (f *FS) Open(name string) (fs.File, error) {
|
||||||
return f.fs.Open(name)
|
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
|
// ReadDir reads []fs.DirEntry
|
||||||
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
|
// This method will prefer EMBEDDED, because that is the "real" FS for overlay
|
||||||
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
||||||
|
@ -66,6 +78,15 @@ func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
||||||
return x, nil
|
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
|
// WithSub sets a fs.Sub for an FS
|
||||||
func WithSub(sub string) Option {
|
func WithSub(sub string) Option {
|
||||||
return func(x *FS) (err error) {
|
return func(x *FS) (err error) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package overlay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"io"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -49,7 +48,7 @@ func TestOverlay(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer fi.Close()
|
defer fi.Close()
|
||||||
|
|
||||||
contents, err := io.ReadAll(fi)
|
contents, err := x.ReadFile(tc.File)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
|
Loading…
Reference in New Issue