This commit is contained in:
Alex Yatskov 2024-04-18 22:19:55 -07:00
parent 42c98df5f7
commit 488ccdebe4
4 changed files with 25 additions and 9 deletions

View File

@ -2,17 +2,24 @@ package sideload
import ( import (
"bytes" "bytes"
"embed" "io/fs"
"path/filepath" "path/filepath"
"git.foosoft.net/alex/goldsmith" "git.foosoft.net/alex/goldsmith"
) )
type Sideload struct { type (
Sideload struct {
files []*goldsmith.File files []*goldsmith.File
fileSystems []embed.FS fileSystems []sideloadFs
} }
sideloadFs interface {
fs.ReadDirFS
fs.ReadFileFS
}
)
func New() *Sideload { func New() *Sideload {
return &Sideload{} return &Sideload{}
} }
@ -45,12 +52,15 @@ func (self *Sideload) Files(files ...*goldsmith.File) *Sideload {
return self return self
} }
func (self *Sideload) FileSystems(fileSystems ...embed.FS) *Sideload { func (self *Sideload) FileSystems(fileSystems ...fs.FS) *Sideload {
self.fileSystems = append(self.fileSystems, fileSystems...) for _, fileSystem := range fileSystems {
self.fileSystems = append(self.fileSystems, fileSystem.(sideloadFs))
}
return self return self
} }
func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem embed.FS, path string) ([]*goldsmith.File, error) { func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem sideloadFs, path string) ([]*goldsmith.File, error) {
entries, err := fileSystem.ReadDir(path) entries, err := fileSystem.ReadDir(path)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,6 +1,7 @@
package sideload package sideload
import ( import (
"io/fs"
"testing" "testing"
"embed" "embed"
@ -13,10 +14,15 @@ import (
var embedFs embed.FS var embedFs embed.FS
func Test(self *testing.T) { func Test(self *testing.T) {
subFs, err := fs.Sub(embedFs, "testdata/source")
if err != nil {
panic(err)
}
harness.Validate( harness.Validate(
self, self,
func(gs *goldsmith.Goldsmith) { func(gs *goldsmith.Goldsmith) {
gs.Chain(New().FileSystems(embedFs)) gs.Chain(New().FileSystems(subFs))
}, },
) )
} }

View File

View File