Initial work on sideload plugin
This commit is contained in:
parent
ab0a930eaa
commit
42c98df5f7
85
plugins/sideload/sideload.go
Normal file
85
plugins/sideload/sideload.go
Normal file
@ -0,0 +1,85 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"path/filepath"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
)
|
||||
|
||||
type Sideload struct {
|
||||
files []*goldsmith.File
|
||||
fileSystems []embed.FS
|
||||
}
|
||||
|
||||
func New() *Sideload {
|
||||
return &Sideload{}
|
||||
}
|
||||
|
||||
func (*Sideload) Name() string {
|
||||
return "sideload"
|
||||
}
|
||||
|
||||
func (self *Sideload) Finalize(context *goldsmith.Context) error {
|
||||
files := self.files
|
||||
|
||||
for _, fileSystem := range self.fileSystems {
|
||||
currFiles, err := self.gatherFsFiles(context, fileSystem, ".")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
files = append(files, currFiles...)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
context.DispatchFile(file)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Sideload) Files(files ...*goldsmith.File) *Sideload {
|
||||
self.files = append(self.files, files...)
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Sideload) FileSystems(fileSystems ...embed.FS) *Sideload {
|
||||
self.fileSystems = append(self.fileSystems, fileSystems...)
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem embed.FS, path string) ([]*goldsmith.File, error) {
|
||||
entries, err := fileSystem.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var files []*goldsmith.File
|
||||
for _, entry := range entries {
|
||||
currPath := filepath.Join(path, entry.Name())
|
||||
if entry.IsDir() {
|
||||
currFiles, err := self.gatherFsFiles(context, fileSystem, currPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files = append(files, currFiles...)
|
||||
} else {
|
||||
data, err := fileSystem.ReadFile(currPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, err := context.CreateFileFromReader(currPath, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files = append(files, file)
|
||||
}
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
22
plugins/sideload/sideload_test.go
Normal file
22
plugins/sideload/sideload_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"embed"
|
||||
|
||||
"git.foosoft.net/alex/goldsmith"
|
||||
"git.foosoft.net/alex/goldsmith/harness"
|
||||
)
|
||||
|
||||
//go:embed testdata/source
|
||||
var embedFs embed.FS
|
||||
|
||||
func Test(self *testing.T) {
|
||||
harness.Validate(
|
||||
self,
|
||||
func(gs *goldsmith.Goldsmith) {
|
||||
gs.Chain(New().FileSystems(embedFs))
|
||||
},
|
||||
)
|
||||
}
|
0
plugins/sideload/testdata/source/1.txt
vendored
Normal file
0
plugins/sideload/testdata/source/1.txt
vendored
Normal file
0
plugins/sideload/testdata/source/child/2.txt
vendored
Normal file
0
plugins/sideload/testdata/source/child/2.txt
vendored
Normal file
Loading…
Reference in New Issue
Block a user