Add ReadFile, interface guard, and drone CI #2

Merged
jolheiser merged 3 commits from read-file into main 2021-05-09 04:02:17 +00:00
4 changed files with 42 additions and 2 deletions

17
.drone.yml 100644
View File

@ -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

View File

@ -1,4 +1,7 @@
# 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 is an easy way to implement a file system in such a way that

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
@ -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) {

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()