Update sideload

This commit is contained in:
Alex Yatskov 2024-04-21 11:38:12 -07:00
parent 8bafe13084
commit 18c519b412
9 changed files with 49 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package sideload
import ( import (
"bytes" "bytes"
"io"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
@ -10,11 +11,16 @@ import (
type ( type (
Sideload struct { Sideload struct {
files []*goldsmith.File files []File
fileSystems []sideloadFs fileSystems []sfs
} }
sideloadFs interface { File struct {
Path string
Reader io.Reader
}
sfs interface {
fs.ReadDirFS fs.ReadDirFS
fs.ReadFileFS fs.ReadFileFS
} }
@ -29,38 +35,43 @@ func (*Sideload) Name() string {
} }
func (self *Sideload) Finalize(context *goldsmith.Context) error { func (self *Sideload) Finalize(context *goldsmith.Context) error {
files := self.files
for _, fileSystem := range self.fileSystems { for _, fileSystem := range self.fileSystems {
currFiles, err := self.gatherFsFiles(context, fileSystem, ".") files, err := self.gatherFsFiles(context, fileSystem, ".")
if err != nil { if err != nil {
return err return err
} }
files = append(files, currFiles...) for _, file := range files {
context.DispatchFile(file)
}
} }
for _, file := range files { for _, file := range self.files {
context.DispatchFile(file) gsFile, err := context.CreateFileFromReader(file.Path, file.Reader)
if err != nil {
return err
}
context.DispatchFile(gsFile)
} }
return nil return nil
} }
func (self *Sideload) Files(files ...*goldsmith.File) *Sideload { func (self *Sideload) Files(files ...File) *Sideload {
self.files = append(self.files, files...) self.files = append(self.files, files...)
return self return self
} }
func (self *Sideload) FileSystems(fileSystems ...fs.FS) *Sideload { func (self *Sideload) FileSystems(fileSystems ...fs.FS) *Sideload {
for _, fileSystem := range fileSystems { for _, fileSystem := range fileSystems {
self.fileSystems = append(self.fileSystems, fileSystem.(sideloadFs)) self.fileSystems = append(self.fileSystems, fileSystem.(sfs))
} }
return self return self
} }
func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem sideloadFs, path string) ([]*goldsmith.File, error) { func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem sfs, 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

@ -2,6 +2,7 @@ package sideload
import ( import (
"io/fs" "io/fs"
"strings"
"testing" "testing"
"embed" "embed"
@ -10,19 +11,35 @@ import (
"git.foosoft.net/alex/goldsmith/harness" "git.foosoft.net/alex/goldsmith/harness"
) )
//go:embed testdata/source //go:embed testdata/fs/source
var embedFs embed.FS var embedFs embed.FS
func Test(self *testing.T) { func TestFs(self *testing.T) {
subFs, err := fs.Sub(embedFs, "testdata/source") harness.ValidateCase(
if err != nil {
panic(err)
}
harness.Validate(
self, self,
"fs",
func(gs *goldsmith.Goldsmith) { func(gs *goldsmith.Goldsmith) {
subFs, err := fs.Sub(embedFs, "testdata/fs/source")
if err != nil {
panic(err)
}
gs.Chain(New().FileSystems(subFs)) gs.Chain(New().FileSystems(subFs))
}, },
) )
} }
func TestFile(self *testing.T) {
harness.ValidateCase(
self,
"file",
func(gs *goldsmith.Goldsmith) {
file := File{
Path: "hello.txt",
Reader: strings.NewReader("hello world"),
}
gs.Chain(New().Files(file))
},
)
}

View File

@ -0,0 +1 @@
hello world

View File

View File