2024-04-19 05:20:26 +00:00
|
|
|
package sideload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-04-21 18:38:12 +00:00
|
|
|
"io"
|
2024-04-19 05:20:26 +00:00
|
|
|
"io/fs"
|
2024-05-06 02:56:16 +00:00
|
|
|
"path"
|
2024-04-19 05:20:26 +00:00
|
|
|
|
|
|
|
"git.foosoft.net/alex/goldsmith"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Sideload struct {
|
2024-04-21 18:38:12 +00:00
|
|
|
files []File
|
|
|
|
fileSystems []sfs
|
2024-04-19 05:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 18:38:12 +00:00
|
|
|
File struct {
|
|
|
|
Path string
|
|
|
|
Reader io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
sfs interface {
|
2024-04-19 05:20:26 +00:00
|
|
|
fs.ReadDirFS
|
|
|
|
fs.ReadFileFS
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func New() *Sideload {
|
|
|
|
return &Sideload{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Sideload) Name() string {
|
|
|
|
return "sideload"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Sideload) Finalize(context *goldsmith.Context) error {
|
|
|
|
for _, fileSystem := range self.fileSystems {
|
2024-04-21 18:38:12 +00:00
|
|
|
files, err := self.gatherFsFiles(context, fileSystem, ".")
|
2024-04-19 05:20:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-21 18:38:12 +00:00
|
|
|
for _, file := range files {
|
|
|
|
context.DispatchFile(file)
|
|
|
|
}
|
2024-04-19 05:20:26 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 18:38:12 +00:00
|
|
|
for _, file := range self.files {
|
|
|
|
gsFile, err := context.CreateFileFromReader(file.Path, file.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.DispatchFile(gsFile)
|
2024-04-19 05:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-21 18:38:12 +00:00
|
|
|
func (self *Sideload) Files(files ...File) *Sideload {
|
2024-04-19 05:20:26 +00:00
|
|
|
self.files = append(self.files, files...)
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Sideload) FileSystems(fileSystems ...fs.FS) *Sideload {
|
|
|
|
for _, fileSystem := range fileSystems {
|
2024-04-21 18:38:12 +00:00
|
|
|
self.fileSystems = append(self.fileSystems, fileSystem.(sfs))
|
2024-04-19 05:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2024-05-06 02:56:16 +00:00
|
|
|
func (self *Sideload) gatherFsFiles(context *goldsmith.Context, fileSystem sfs, searchPath string) ([]*goldsmith.File, error) {
|
|
|
|
entries, err := fileSystem.ReadDir(searchPath)
|
2024-04-19 05:20:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []*goldsmith.File
|
|
|
|
for _, entry := range entries {
|
2024-05-06 02:56:16 +00:00
|
|
|
currPath := path.Join(searchPath, entry.Name())
|
2024-04-19 05:20:26 +00:00
|
|
|
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
|
|
|
|
}
|