Add ReadFile, interface guard, and drone CI
Signed-off-by: jolheiser <john.olheiser@gmail.com>pull/2/head
parent
ea7687d011
commit
e4c2d4f9a1
|
@ -0,0 +1,18 @@
|
|||
---
|
||||
kind: pipeline
|
||||
name: compliance
|
||||
trigger:
|
||||
event:
|
||||
- pull_request
|
||||
steps:
|
||||
- name: build
|
||||
pull: always
|
||||
image: golang:1.16
|
||||
commands:
|
||||
- make test
|
||||
- make build
|
||||
- name: check
|
||||
pull: always
|
||||
image: golang:1.16
|
||||
commands:
|
||||
- make vet
|
21
overlay.go
21
overlay.go
|
@ -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
|
||||
|
@ -39,6 +43,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) {
|
||||
|
@ -66,6 +78,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) {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue