diff --git a/plugins/sideload/sideload.go b/plugins/sideload/sideload.go index 858af9a..2955875 100644 --- a/plugins/sideload/sideload.go +++ b/plugins/sideload/sideload.go @@ -2,6 +2,7 @@ package sideload import ( "bytes" + "io" "io/fs" "path/filepath" @@ -10,11 +11,16 @@ import ( type ( Sideload struct { - files []*goldsmith.File - fileSystems []sideloadFs + files []File + fileSystems []sfs } - sideloadFs interface { + File struct { + Path string + Reader io.Reader + } + + sfs interface { fs.ReadDirFS fs.ReadFileFS } @@ -29,38 +35,43 @@ func (*Sideload) Name() string { } func (self *Sideload) Finalize(context *goldsmith.Context) error { - files := self.files - for _, fileSystem := range self.fileSystems { - currFiles, err := self.gatherFsFiles(context, fileSystem, ".") + files, err := self.gatherFsFiles(context, fileSystem, ".") if err != nil { return err } - files = append(files, currFiles...) + for _, file := range files { + context.DispatchFile(file) + } } - for _, file := range files { - context.DispatchFile(file) + for _, file := range self.files { + gsFile, err := context.CreateFileFromReader(file.Path, file.Reader) + if err != nil { + return err + } + + context.DispatchFile(gsFile) } return nil } -func (self *Sideload) Files(files ...*goldsmith.File) *Sideload { +func (self *Sideload) Files(files ...File) *Sideload { self.files = append(self.files, files...) return self } func (self *Sideload) FileSystems(fileSystems ...fs.FS) *Sideload { for _, fileSystem := range fileSystems { - self.fileSystems = append(self.fileSystems, fileSystem.(sideloadFs)) + self.fileSystems = append(self.fileSystems, fileSystem.(sfs)) } 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) if err != nil { return nil, err diff --git a/plugins/sideload/sideload_test.go b/plugins/sideload/sideload_test.go index eac1685..28015ef 100644 --- a/plugins/sideload/sideload_test.go +++ b/plugins/sideload/sideload_test.go @@ -2,6 +2,7 @@ package sideload import ( "io/fs" + "strings" "testing" "embed" @@ -10,19 +11,35 @@ import ( "git.foosoft.net/alex/goldsmith/harness" ) -//go:embed testdata/source +//go:embed testdata/fs/source var embedFs embed.FS -func Test(self *testing.T) { - subFs, err := fs.Sub(embedFs, "testdata/source") - if err != nil { - panic(err) - } - - harness.Validate( +func TestFs(self *testing.T) { + harness.ValidateCase( self, + "fs", func(gs *goldsmith.Goldsmith) { + subFs, err := fs.Sub(embedFs, "testdata/fs/source") + if err != nil { + panic(err) + } + 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)) + }, + ) +} diff --git a/plugins/sideload/testdata/file/reference/hello.txt b/plugins/sideload/testdata/file/reference/hello.txt new file mode 100644 index 0000000..95d09f2 --- /dev/null +++ b/plugins/sideload/testdata/file/reference/hello.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/plugins/sideload/testdata/reference/1.txt b/plugins/sideload/testdata/file/reference/test.txt similarity index 100% rename from plugins/sideload/testdata/reference/1.txt rename to plugins/sideload/testdata/file/reference/test.txt diff --git a/plugins/sideload/testdata/reference/child/2.txt b/plugins/sideload/testdata/file/source/test.txt similarity index 100% rename from plugins/sideload/testdata/reference/child/2.txt rename to plugins/sideload/testdata/file/source/test.txt diff --git a/plugins/sideload/testdata/source/1.txt b/plugins/sideload/testdata/fs/reference/1.txt similarity index 100% rename from plugins/sideload/testdata/source/1.txt rename to plugins/sideload/testdata/fs/reference/1.txt diff --git a/plugins/sideload/testdata/source/child/2.txt b/plugins/sideload/testdata/fs/reference/child/2.txt similarity index 100% rename from plugins/sideload/testdata/source/child/2.txt rename to plugins/sideload/testdata/fs/reference/child/2.txt diff --git a/plugins/sideload/testdata/fs/source/1.txt b/plugins/sideload/testdata/fs/source/1.txt new file mode 100644 index 0000000..e69de29 diff --git a/plugins/sideload/testdata/fs/source/child/2.txt b/plugins/sideload/testdata/fs/source/child/2.txt new file mode 100644 index 0000000..e69de29