pkger/pkging/mem/add.go

105 lines
1.6 KiB
Go
Raw Normal View History

2019-10-21 00:52:16 +03:00
package mem
import (
"bytes"
"io"
2019-10-24 23:22:15 +03:00
"os"
2019-10-21 00:52:16 +03:00
"path/filepath"
2019-12-03 23:07:51 +03:00
"github.com/gobuffalo/here"
2019-10-21 00:52:16 +03:00
"github.com/markbates/pkger/pkging"
)
2019-10-24 23:22:15 +03:00
var _ pkging.Adder = &Pkger{}
2019-10-21 00:52:16 +03:00
// Add copies the pkging.File into the *Pkger
2019-10-24 23:22:15 +03:00
func (fx *Pkger) Add(files ...*os.File) error {
2019-10-21 00:52:16 +03:00
for _, f := range files {
info, err := f.Stat()
if err != nil {
return err
}
2019-10-24 23:22:15 +03:00
pt, err := fx.Parse(f.Name())
if err != nil {
return err
}
dir := f.Name()
if !info.IsDir() {
dir = filepath.Dir(dir)
}
her, err := here.Dir(dir)
2019-10-24 23:22:15 +03:00
if err != nil {
return err
}
if info.IsDir() {
err = filepath.Walk(f.Name(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
f, err := os.Open(path)
if err != nil {
2019-10-21 00:52:16 +03:00
return err
}
2019-10-24 23:22:15 +03:00
defer f.Close()
pt, err := fx.Parse(path)
if err != nil {
return err
}
her, err := here.Package(pt.Pkg)
if err != nil {
return err
}
mf := &File{
Here: her,
info: pkging.NewFileInfo(info),
path: pt,
pkging: fx,
}
if !info.IsDir() {
bb := &bytes.Buffer{}
_, err = io.Copy(bb, f)
if err != nil {
return err
}
mf.data = bb.Bytes()
}
fx.files.Store(mf.Path(), mf)
return nil
})
if err != nil {
return err
2019-10-21 00:52:16 +03:00
}
2019-10-24 23:22:15 +03:00
continue
2019-10-21 00:52:16 +03:00
}
mf := &File{
2019-10-24 23:22:15 +03:00
Here: her,
2019-10-21 00:52:16 +03:00
info: pkging.NewFileInfo(info),
2019-10-24 23:22:15 +03:00
path: pt,
2019-10-21 00:52:16 +03:00
pkging: fx,
}
if !info.IsDir() {
bb := &bytes.Buffer{}
_, err = io.Copy(bb, f)
if err != nil {
return err
}
mf.data = bb.Bytes()
}
fx.files.Store(mf.Path(), mf)
}
return nil
}