diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index 15b8b0f..eda283e 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -1 +1,52 @@ package costello + +import ( + "io" + "os" + "path/filepath" + "strings" + + "github.com/markbates/pkger/pkging" +) + +func LoadRef(ref *Ref, pkg pkging.Pkger) error { + return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + of, err := os.Open(path) + if err != nil { + return err + } + defer of.Close() + + if a, ok := pkg.(pkging.Adder); ok { + return a.Add(of) + } + + path = strings.TrimPrefix(path, ref.Dir) + + pt, err := pkg.Parse(path) + if err != nil { + return err + } + + if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil { + return err + } + + if info.IsDir() { + return nil + } + f, err := pkg.Create(pt.String()) + if err != nil { + return err + } + defer f.Close() + + if _, err := io.Copy(f, of); err != nil { + return err + } + return nil + }) +} diff --git a/pkging/costello/open.go b/pkging/costello/open.go index 15b8b0f..563b3eb 100644 --- a/pkging/costello/open.go +++ b/pkging/costello/open.go @@ -1 +1,45 @@ package costello + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + "time" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func OpenTest(t *testing.T, pkg pkging.Pkger) { + r := require.New(t) + + app, err := NewRef() + r.NoError(err) + + osf, err := os.Open(filepath.Join(app.Dir, "go.mod")) + r.NoError(err) + + osi, err := osf.Stat() + r.NoError(err) + + osb, err := ioutil.ReadAll(osf) + r.NoError(err) + r.NoError(osf.Close()) + + pf, err := pkg.Open("/go.mod") + r.NoError(err) + + psi, err := pf.Stat() + r.NoError(err) + + psb, err := ioutil.ReadAll(pf) + r.NoError(err) + r.NoError(pf.Close()) + + r.Equal(osi.Name(), psi.Name()) + r.Equal(osi.Mode(), psi.Mode()) + r.Equal(osi.Size(), psi.Size()) + r.Equal(osi.ModTime().Format(time.RFC3339), psi.ModTime().Format(time.RFC3339)) + r.Equal(osb, psb) +} diff --git a/pkging/mem/add.go b/pkging/mem/add.go index 12470c5..56fe549 100644 --- a/pkging/mem/add.go +++ b/pkging/mem/add.go @@ -25,7 +25,12 @@ func (fx *Pkger) Add(files ...*os.File) error { return err } - her, err := here.Package(pt.Pkg) + dir := f.Name() + if !info.IsDir() { + dir = filepath.Dir(dir) + } + + her, err := here.Dir(dir) if err != nil { return err } diff --git a/pkging/mem/create_test.go b/pkging/mem/create_test.go new file mode 100644 index 0000000..4ee879b --- /dev/null +++ b/pkging/mem/create_test.go @@ -0,0 +1,23 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Open(t *testing.T) { + r := require.New(t) + + app, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(app.Info) + r.NoError(err) + + err = costello.LoadRef(app, pkg) + r.NoError(err) + + costello.OpenTest(t, pkg) +}