diff --git a/Makefile b/Makefile index 94018ac..304b497 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,14 @@ TAGS ?= "" GO_BIN ?= "go" + install: tidy cd ./cmd/pkger && $(GO_BIN) install -tags ${TAGS} -v . make tidy +run: install + cd ./examples/app; pkger + tidy: $(GO_BIN) mod tidy -v diff --git a/apply.go b/apply.go new file mode 100644 index 0000000..e90b1d8 --- /dev/null +++ b/apply.go @@ -0,0 +1,20 @@ +package pkger + +import ( + "sync" + + "github.com/markbates/pkger/pkging" +) + +var current pkging.Pkger +var gil = &sync.RWMutex{} + +func Apply(pkg pkging.Pkger, err error) error { + if err != nil { + return err + } + gil.Lock() + defer gil.Unlock() + current = pkging.Wrap(current, pkg) + return nil +} diff --git a/cmd/pkger/main.go b/cmd/pkger/main.go index ad9ba16..f2d16ae 100644 --- a/cmd/pkger/main.go +++ b/cmd/pkger/main.go @@ -31,4 +31,4 @@ func run() error { return root.Route(os.Args[1:]) } -// does not computee +// does not compute diff --git a/cmd/pkger/pack.go b/cmd/pkger/pack.go index b5a7e55..4a10f46 100644 --- a/cmd/pkger/pack.go +++ b/cmd/pkger/pack.go @@ -9,6 +9,7 @@ import ( "github.com/markbates/pkger" "github.com/markbates/pkger/parser" "github.com/markbates/pkger/pkging" + "github.com/markbates/pkger/stuffing" ) const outName = "pkged.go" @@ -124,6 +125,7 @@ func Package(out string, paths []pkging.Path) error { if err != nil { return err } + defer f.Close() c, err := pkger.Current() if err != nil { @@ -131,14 +133,15 @@ func Package(out string, paths []pkging.Path) error { } fmt.Fprintf(f, "package %s\n\n", c.Name) fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n") - fmt.Fprintf(f, "var _ = pkger.Unpack(`") + fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n") - // TODO - // if err := pkger.Pack(f, paths); err != nil { - // return err - // } + fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") - fmt.Fprintf(f, "`)\n") + if err := stuffing.Stuff(f, c, paths); err != nil { + return err + } + + fmt.Fprintf(f, "`)))\n") return nil } diff --git a/cmd/pkger/walk.go b/cmd/pkger/walk.go index 907f041..ef844e4 100644 --- a/cmd/pkger/walk.go +++ b/cmd/pkger/walk.go @@ -8,7 +8,7 @@ import ( ) func walk(args []string) error { - err := pkger.Walk(".", func(path string, info os.FileInfo, err error) error { + err := pkger.Walk("/", func(path string, info os.FileInfo, err error) error { if err != nil { return err } diff --git a/examples/app/.gitignore b/examples/app/.gitignore index 294b4e0..ef4215f 100644 --- a/examples/app/.gitignore +++ b/examples/app/.gitignore @@ -1,2 +1,3 @@ example app +pkged.go diff --git a/examples/app/Makefile b/examples/app/Makefile index 48658d4..0f86733 100644 --- a/examples/app/Makefile +++ b/examples/app/Makefile @@ -1,5 +1,5 @@ default: - cd ../../../cmd/pkger && go install -v . + cd ../../cmd/pkger && go install -v . pkger GOOS=linux go build -v -o example docker build -t pkger:example . diff --git a/go.mod b/go.mod index 74293c9..086da1b 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,6 @@ module github.com/markbates/pkger go 1.13 require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/pretty v0.1.0 // indirect github.com/markbates/errx v1.1.0 github.com/stretchr/testify v1.4.0 - gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index c6cc8bc..fee900f 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,5 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI= github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -16,7 +9,5 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/here/dir.go b/here/dir.go index d0d1034..3070058 100644 --- a/here/dir.go +++ b/here/dir.go @@ -8,7 +8,7 @@ import ( // Dir attempts to gather info for the requested directory. func Dir(p string) (Info, error) { - return Cache(p, func(p string) (Info, error) { + i, err := Cache(p, func(p string) (Info, error) { var i Info fi, err := os.Stat(p) @@ -40,4 +40,14 @@ func Dir(p string) (Info, error) { return i, nil }) + + if err != nil { + return i, err + } + + Cache(i.ImportPath, func(p string) (Info, error) { + return i, nil + }) + + return i, nil } diff --git a/here/pkg.go b/here/pkg.go index 3a2bc3c..9e599bf 100644 --- a/here/pkg.go +++ b/here/pkg.go @@ -14,7 +14,7 @@ import ( // returned `Info` value and pass it to the `Dir(string) (Info, error)` // function to return the complete data. func Package(p string) (Info, error) { - return Cache(p, func(p string) (Info, error) { + i, err := Cache(p, func(p string) (Info, error) { var i Info b, err := run("go", "list", "-json", "-find", p) if err != nil { @@ -26,4 +26,15 @@ func Package(p string) (Info, error) { return i, nil }) + + if err != nil { + return i, err + } + + Cache(i.Dir, func(p string) (Info, error) { + return i, nil + }) + + return i, nil + } diff --git a/parser/parser.go b/parser/parser.go index 7509fdf..f152c71 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -14,10 +14,10 @@ import ( var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"} -func Parse(cur here.Info) (Results, error) { +func Parse(her here.Info) (Results, error) { var r Results - name := cur.ImportPath + name := her.ImportPath pt, err := pkger.Parse(name) if err != nil { @@ -25,12 +25,6 @@ func Parse(cur here.Info) (Results, error) { } r.Path = pt - her, err := pkger.Info(r.Path.Pkg) - - if err != nil { - return r, err - } - m := map[pkging.Path]bool{} root := r.Path.Name diff --git a/parser/parser_test.go b/parser/parser_test.go index 5946b89..3355bdd 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -1,6 +1,7 @@ package parser import ( + "fmt" "path/filepath" "sort" "testing" @@ -12,7 +13,9 @@ import ( func Test_Parser(t *testing.T) { r := require.New(t) - ch := filepath.Join("..", "examples", "app") + ch := filepath.Join("..", + "examples", + "app") info := here.Info{ Dir: ch, ImportPath: "github.com/markbates/pkger/examples/app", @@ -24,15 +27,12 @@ func Test_Parser(t *testing.T) { exp := []string{ "github.com/markbates/pkger/examples/app:/", "github.com/markbates/pkger/examples/app:/public", - "github.com/markbates/pkger/examples/app:/templates", - "github.com/markbates/pkger/examples/app:/templates/a.txt", - "github.com/markbates/pkger/examples/app:/templates/b", "github.com/markbates/pkger/examples/app:/public/images/mark-small.png", - "github.com/markbates/pkger/examples/app:/public/images", "github.com/markbates/pkger/examples/app:/public/images/mark.png", "github.com/markbates/pkger/examples/app:/public/images/mark_250px.png", "github.com/markbates/pkger/examples/app:/public/images/mark_400px.png", "github.com/markbates/pkger/examples/app:/public/index.html", + "github.com/markbates/pkger/examples/app:/templates/a.txt", } sort.Strings(exp) @@ -42,5 +42,6 @@ func Test_Parser(t *testing.T) { } sort.Strings(act) + fmt.Printf("%#v\n", act) r.Equal(exp, act) } diff --git a/pkger.go b/pkger.go index 326e3ac..0a100da 100644 --- a/pkger.go +++ b/pkger.go @@ -2,28 +2,133 @@ package pkger import ( "log" + "os" + "path/filepath" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging/stdos" ) -var current = func() pkging.Pkger { +var disk = func() pkging.Pkger { n, err := stdos.New() if err != nil { - log.Fatal(err) + log.Println(err) } return n }() -var Abs = current.Abs -var AbsPath = current.AbsPath -var Create = current.Create -var Current = current.Current -var Info = current.Info -var MkdirAll = current.MkdirAll -var Open = current.Open -var Parse = current.Parse -var Remove = current.Remove -var RemoveAll = current.RemoveAll -var Stat = current.Stat -var Walk = current.Walk +func Parse(p string) (pkging.Path, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Parse(p) + } + return current.Parse(p) +} + +func Abs(p string) (string, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Abs(p) + } + return current.Abs(p) +} + +func AbsPath(p pkging.Path) (string, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.AbsPath(p) + } + return current.AbsPath(p) +} + +func Current() (here.Info, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Current() + } + return current.Current() +} + +func Info(p string) (here.Info, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Info(p) + } + return current.Info(p) +} + +// Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. +func Create(p string) (pkging.File, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Create(p) + } + return current.Create(p) +} + +// MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil. +func MkdirAll(p string, perm os.FileMode) error { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.MkdirAll(p, perm) + } + return current.MkdirAll(p, perm) +} + +// Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. +func Open(p string) (pkging.File, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Open(p) + } + return current.Open(p) +} + +// Stat returns a FileInfo describing the named file. +func Stat(name string) (os.FileInfo, error) { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Stat(name) + } + return current.Stat(name) +} + +// Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links. - That is from the standard library. I know. Their grammar teachers can not be happy with them right now. +func Walk(p string, wf filepath.WalkFunc) error { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Walk(p, wf) + } + return current.Walk(p, wf) +} + +// Remove removes the named file or (empty) directory. +func Remove(name string) error { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.Remove(name) + } + return current.Remove(name) +} + +// RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error). +func RemoveAll(name string) error { + gil.RLock() + defer gil.RUnlock() + if current == nil { + return disk.RemoveAll(name) + } + return current.RemoveAll(name) +} diff --git a/pkging/mem/embed.go b/pkging/mem/embed.go new file mode 100644 index 0000000..f5c471b --- /dev/null +++ b/pkging/mem/embed.go @@ -0,0 +1,44 @@ +package mem + +import ( + "bytes" + "compress/gzip" + "encoding/hex" + "encoding/json" + + "github.com/markbates/pkger/pkging" +) + +func (pkg *Pkger) MarshalEmbed() ([]byte, error) { + bb := &bytes.Buffer{} + gz := gzip.NewWriter(bb) + defer gz.Close() + if err := json.NewEncoder(gz).Encode(pkg); err != nil { + return nil, err + } + if err := gz.Close(); err != nil { + return nil, err + } + s := hex.EncodeToString(bb.Bytes()) + return []byte(s), nil +} + +func UnmarshalEmbed(in []byte) (pkging.Pkger, error) { + b := make([]byte, len(in)) + if _, err := hex.Decode(b, in); err != nil { + return nil, err + } + + gz, err := gzip.NewReader(bytes.NewReader(b)) + if err != nil { + return nil, err + } + defer gz.Close() + + pkg := &Pkger{} + if err := json.NewDecoder(gz).Decode(pkg); err != nil { + return nil, err + } + + return pkg, nil +} diff --git a/pkging/mem/embed_test.go b/pkging/mem/embed_test.go new file mode 100644 index 0000000..f75af46 --- /dev/null +++ b/pkging/mem/embed_test.go @@ -0,0 +1,51 @@ +package mem + +import ( + "fmt" + "os" + "testing" + + "github.com/markbates/pkger/here" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_MarshalEmbed(t *testing.T) { + r := require.New(t) + + info, err := here.Current() + r.NoError(err) + + pkg, err := New(info) + r.NoError(err) + + const N = 10 + for i := 0; i < N; i++ { + name := fmt.Sprintf("/%d.txt", i) + f, err := pkg.Create(name) + r.NoError(err) + f.Write([]byte(name)) + r.NoError(f.Close()) + } + + em, err := pkg.MarshalEmbed() + r.NoError(err) + + p2, err := UnmarshalEmbed(em) + r.NoError(err) + + pinfo, err := p2.Current() + r.NoError(err) + r.Equal(info, pinfo) + + var act []os.FileInfo + err = p2.Walk("/", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + act = append(act, info) + + return nil + }) + r.NoError(err) + r.Len(act, N+1) // +1 for the / dir +} diff --git a/pkging/mem/file.go b/pkging/mem/file.go index a70d076..79bce9e 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -2,6 +2,7 @@ package mem import ( "bytes" + "encoding/json" "fmt" "io" "net/http" @@ -30,6 +31,37 @@ type File struct { pkging pkging.Pkger } +type fJay struct { + Info *pkging.FileInfo `json:"info"` + Her here.Info `json:"her"` + Path pkging.Path `json:"path"` + Data []byte `json:"data"` + Parent pkging.Path `json:"parent"` +} + +func (f File) MarshalJSON() ([]byte, error) { + return json.Marshal(fJay{ + Info: f.info, + Her: f.her, + Path: f.path, + Data: f.data, + Parent: f.parent, + }) +} + +func (f *File) UnmarshalJSON(b []byte) error { + var y fJay + if err := json.Unmarshal(b, &y); err != nil { + return err + } + f.info = y.Info + f.her = y.Her + f.path = y.Path + f.data = y.Data + f.parent = y.Parent + return nil +} + func (f *File) Seek(ofpkginget int64, whence int) (int64, error) { if sk, ok := f.reader.(io.Seeker); ok { return sk.Seek(ofpkginget, whence) @@ -205,17 +237,6 @@ func (f *File) Open(name string) (http.File, error) { return di, nil } -// func (f File) MarshalJSON() ([]byte, error) { -// m := map[string]interface{}{ -// "info": f.info, -// "her": f.her, -// "path": f.path, -// "data": f.data, -// "parent": f.parent, -// } -// return json.Marshal(m) -// } - // func (f *File) UnmarshalJSON(b []byte) error { // m := map[string]json.RawMessage{} // if err := json.Unmarshal(b, &m); err != nil { diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index 8f4ff97..1ced4c5 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -1,7 +1,10 @@ package mem import ( + "encoding/json" "fmt" + "io" + "io/ioutil" "os" "path/filepath" "strings" @@ -41,6 +44,58 @@ type Pkger struct { current here.Info } +type jay struct { + Infos *maps.Infos `json:"infos"` + Paths *maps.Paths `json:"paths"` + Files map[string]*File `json:"files"` + Current here.Info `json:"current"` +} + +func (p *Pkger) MarshalJSON() ([]byte, error) { + files := map[string]*File{} + + p.files.Range(func(key pkging.Path, file pkging.File) bool { + f, ok := file.(*File) + if !ok { + return true + } + files[key.String()] = f + return true + }) + + return json.Marshal(jay{ + Infos: p.infos, + Paths: p.paths, + Files: files, + Current: p.current, + }) +} + +func (p *Pkger) UnmarshalJSON(b []byte) error { + y := jay{} + + if err := json.Unmarshal(b, &y); err != nil { + return err + } + + p.current = y.Current + + p.infos = y.Infos + + p.paths = y.Paths + p.paths.Current = p.current + + p.files = &maps.Files{} + for k, v := range y.Files { + pt, err := p.Parse(k) + if err != nil { + return err + } + p.files.Store(pt, v) + } + return nil +} + func (f *Pkger) Abs(p string) (string, error) { pt, err := f.Parse(p) if err != nil { @@ -106,6 +161,33 @@ func (fx *Pkger) RemoveAll(name string) error { return nil } +func (fx *Pkger) Add(info os.FileInfo, r io.Reader) error { + dir := filepath.Dir(info.Name()) + fx.MkdirAll(dir, 0755) + + f, err := fx.Create(info.Name()) + if err != nil { + return err + } + + mf, ok := f.(*File) + if !ok { + return fmt.Errorf("could not add %T", f) + } + + mf.info = pkging.NewFileInfo(info) + + if !mf.info.IsDir() { + b, err := ioutil.ReadAll(r) + if err != nil { + return err + } + mf.data = b + } + fx.files.Store(f.Path(), f) + return nil +} + func (fx *Pkger) Create(name string) (pkging.File, error) { fx.MkdirAll("/", 0755) pt, err := fx.Parse(name) diff --git a/pkging/pkger.go b/pkging/pkger.go index 19757b5..1ecb8e2 100644 --- a/pkging/pkger.go +++ b/pkging/pkger.go @@ -9,7 +9,7 @@ import ( type Pkger interface { Parse(p string) (Path, error) - Abs(string) (string, error) + Abs(p string) (string, error) AbsPath(Path) (string, error) Current() (here.Info, error) diff --git a/pkging/wrap.go b/pkging/wrap.go new file mode 100644 index 0000000..5c40519 --- /dev/null +++ b/pkging/wrap.go @@ -0,0 +1,159 @@ +package pkging + +import ( + "os" + "path/filepath" + + "github.com/markbates/pkger/here" +) + +func Wrap(parent, with Pkger) Pkger { + return withPkger{ + base: with, + parent: parent, + } +} + +type withPkger struct { + base Pkger + parent Pkger +} + +func (w withPkger) Parse(p string) (Path, error) { + pt, err := w.base.Parse(p) + if err != nil { + if w.parent != nil { + return w.parent.Parse(p) + } + return pt, err + } + return pt, nil +} + +func (w withPkger) Abs(p string) (string, error) { + pt, err := w.base.Abs(p) + if err != nil { + if w.parent != nil { + return w.parent.Abs(p) + } + return pt, err + } + return pt, nil +} + +func (w withPkger) AbsPath(p Path) (string, error) { + pt, err := w.base.AbsPath(p) + if err != nil { + if w.parent != nil { + return w.parent.AbsPath(p) + } + return pt, err + } + return pt, nil +} + +func (w withPkger) Current() (here.Info, error) { + pt, err := w.base.Current() + if err != nil { + if w.parent != nil { + return w.parent.Current() + } + return pt, err + } + return pt, nil +} + +func (w withPkger) Info(p string) (here.Info, error) { + pt, err := w.base.Info(p) + if err != nil { + if w.parent != nil { + return w.parent.Info(p) + } + return pt, err + } + return pt, nil +} + +// Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. +func (w withPkger) Create(p string) (File, error) { + pt, err := w.base.Create(p) + if err != nil { + if w.parent != nil { + return w.parent.Create(p) + } + return pt, err + } + return pt, nil +} + +// MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil. +func (w withPkger) MkdirAll(p string, perm os.FileMode) error { + err := w.base.MkdirAll(p, perm) + if err != nil { + return err + } + if w.parent != nil { + return w.parent.MkdirAll(p, perm) + } + return nil +} + +// Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. +func (w withPkger) Open(p string) (File, error) { + pt, err := w.base.Open(p) + if err != nil { + if w.parent != nil { + return w.parent.Open(p) + } + return pt, err + } + return pt, nil +} + +// Stat returns a FileInfo describing the named file. +func (w withPkger) Stat(p string) (os.FileInfo, error) { + pt, err := w.base.Stat(p) + if err != nil { + if w.parent != nil { + return w.parent.Stat(p) + } + return pt, err + } + return pt, nil +} + +// Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links. - That is from the standard library. I know. Their grammar teachers can not be happy with them right now. +func (w withPkger) Walk(p string, wf filepath.WalkFunc) error { + err := w.base.Walk(p, wf) + if err != nil { + return err + } + if w.parent != nil { + return w.parent.Walk(p, wf) + } + return nil +} + +// Remove removes the named file or (empty) directory. +func (w withPkger) Remove(p string) error { + err := w.base.Remove(p) + if err != nil { + return err + } + if w.parent != nil { + return w.parent.Remove(p) + } + return nil +} + +// RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error). +func (w withPkger) RemoveAll(p string) error { + err := w.base.RemoveAll(p) + if err != nil { + return err + } + if w.parent != nil { + return w.parent.RemoveAll(p) + } + return nil +} diff --git a/stuffing/stuffing.go b/stuffing/stuffing.go new file mode 100644 index 0000000..3929c74 --- /dev/null +++ b/stuffing/stuffing.go @@ -0,0 +1,54 @@ +package stuffing + +import ( + "io" + + "github.com/markbates/pkger/here" + "github.com/markbates/pkger/pkging" + "github.com/markbates/pkger/pkging/mem" + "github.com/markbates/pkger/pkging/stdos" +) + +func Stuff(w io.Writer, cur here.Info, paths []pkging.Path) error { + disk, err := stdos.New() + if err != nil { + return err + } + + pkg, err := mem.New(cur) + if err != nil { + return err + } + + for _, pt := range paths { + err = func() error { + f, err := disk.Open(pt.String()) + if err != nil { + return err + } + defer f.Close() + + fi, err := f.Stat() + if err != nil { + return err + } + + if err := pkg.Add(fi, f); err != nil { + return err + } + + return nil + // WithInfo(ng, og) + }() + if err != nil { + return err + } + } + + b, err := pkg.MarshalEmbed() + if err != nil { + return err + } + _, err = w.Write(b) + return err +}