Update sideload
This commit is contained in:
parent
8bafe13084
commit
18c519b412
@ -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
|
||||
|
@ -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))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
1
plugins/sideload/testdata/file/reference/hello.txt
vendored
Normal file
1
plugins/sideload/testdata/file/reference/hello.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
hello world
|
0
plugins/sideload/testdata/fs/source/1.txt
vendored
Normal file
0
plugins/sideload/testdata/fs/source/1.txt
vendored
Normal file
0
plugins/sideload/testdata/fs/source/child/2.txt
vendored
Normal file
0
plugins/sideload/testdata/fs/source/child/2.txt
vendored
Normal file
Loading…
Reference in New Issue
Block a user