Add ReadFile, interface guard, and drone CI

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/2/head
jolheiser 2021-05-08 22:54:36 -05:00
parent ea7687d011
commit e4c2d4f9a1
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
3 changed files with 40 additions and 2 deletions

18
.drone.yml 100644
View File

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

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