From ae683c264822e0053bf9eaed6c001e4d3a3be993 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 12:51:29 -0400 Subject: [PATCH 01/10] shine on me --- Makefile | 4 + apply.go | 20 +++++ cmd/pkger/main.go | 2 +- cmd/pkger/pack.go | 15 ++-- cmd/pkger/walk.go | 2 +- examples/app/.gitignore | 1 + examples/app/Makefile | 2 +- go.mod | 3 - go.sum | 9 --- here/dir.go | 12 ++- here/pkg.go | 13 +++- parser/parser.go | 10 +-- parser/parser_test.go | 11 +-- pkger.go | 133 ++++++++++++++++++++++++++++---- pkging/mem/embed.go | 44 +++++++++++ pkging/mem/embed_test.go | 51 +++++++++++++ pkging/mem/file.go | 43 ++++++++--- pkging/mem/mem.go | 82 ++++++++++++++++++++ pkging/pkger.go | 2 +- pkging/wrap.go | 159 +++++++++++++++++++++++++++++++++++++++ stuffing/stuffing.go | 54 +++++++++++++ 21 files changed, 610 insertions(+), 62 deletions(-) create mode 100644 apply.go create mode 100644 pkging/mem/embed.go create mode 100644 pkging/mem/embed_test.go create mode 100644 pkging/wrap.go create mode 100644 stuffing/stuffing.go 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 +} From 60d46437f30a126491ad6be4cde3186079365762 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 12:59:13 -0400 Subject: [PATCH 02/10] bit off more --- examples/app/go.sum | 9 --------- pkging/info.go | 7 ++----- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/examples/app/go.sum b/examples/app/go.sum index 499657f..1945221 100644 --- a/examples/app/go.sum +++ b/examples/app/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/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -15,7 +8,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/pkging/info.go b/pkging/info.go index 9d5f9b5..8ec4e3b 100644 --- a/pkging/info.go +++ b/pkging/info.go @@ -78,13 +78,10 @@ func WithName(name string, info os.FileInfo) *FileInfo { func WithRelName(name string, info os.FileInfo) *FileInfo { s := cleanName(name) - - if !strings.HasPrefix(s, "/") { - s = "/" + s - } + s = strings.TrimPrefix(s, "/") fo := NewFileInfo(info) - fo.Details.Name = cleanName(name) + fo.Details.Name = cleanName(s) return fo } From 88c40da8a1a4296961cc36ce75892e6f3c635f60 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 14:05:59 -0400 Subject: [PATCH 03/10] working for a living --- pkging/mem/file.go | 4 ++++ pkging/pkgtest/file.go | 45 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pkging/mem/file.go b/pkging/mem/file.go index 79bce9e..8bb1c07 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -174,6 +174,10 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) { return io.EOF } + if root == path { + return nil + } + pt, err := f.pkging.Parse(path) if err != nil { return err diff --git a/pkging/pkgtest/file.go b/pkging/pkgtest/file.go index 72d7fa6..313160b 100644 --- a/pkging/pkgtest/file.go +++ b/pkging/pkgtest/file.go @@ -2,6 +2,7 @@ package pkgtest import ( "path/filepath" + "sort" "testing" "github.com/markbates/pkger/pkging/pkgutil" @@ -48,9 +49,47 @@ func (s Suite) Test_File_Info(t *testing.T) { // panic("not implemented") // } // -// func (s Suite) Test_File_Readdir(t *testing.T) { -// panic("not implemented") -// } +func (s Suite) Test_File_Readdir(t *testing.T) { + r := require.New(t) + + pkg, err := s.Make() + r.NoError(err) + + cur, err := pkg.Current() + r.NoError(err) + + ip := cur.ImportPath + table := []struct { + in string + }{ + {in: ":/public"}, + {in: ip + ":/public"}, + } + + r.NoError(s.LoadFolder(pkg)) + + for _, tt := range table { + s.Run(t, tt.in, func(st *testing.T) { + r := require.New(st) + + dir, err := pkg.Open(tt.in) + r.NoError(err) + defer dir.Close() + + infos, err := dir.Readdir(-1) + r.NoError(err) + r.Len(infos, 2) + + sort.Slice(infos, func(i, j int) bool { + return infos[i].Name() < infos[j].Name() + }) + + r.Equal("images", infos[0].Name()) + r.Equal("index.html", infos[1].Name()) + }) + } +} + // // func (s Suite) Test_File_Seek(t *testing.T) { // panic("not implemented") From 175eee360017df32bb4a821aac16a3b1fa6d3633 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 14:42:07 -0400 Subject: [PATCH 04/10] crueler --- pkging/pkgtest/file.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkging/pkgtest/file.go b/pkging/pkgtest/file.go index 313160b..52d82fc 100644 --- a/pkging/pkgtest/file.go +++ b/pkging/pkgtest/file.go @@ -86,6 +86,20 @@ func (s Suite) Test_File_Readdir(t *testing.T) { r.Equal("images", infos[0].Name()) r.Equal("index.html", infos[1].Name()) + + dir, err = pkg.Open(tt.in + "/images") + r.NoError(err) + + infos, err = dir.Readdir(-1) + r.NoError(err) + r.Len(infos, 1) + + sort.Slice(infos, func(i, j int) bool { + return infos[i].Name() < infos[j].Name() + }) + + r.Equal("mark.png", infos[0].Name()) + }) } } From e8a8d77764989949a052b98d75b24d2ccf1481c0 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 17:07:49 -0400 Subject: [PATCH 05/10] like lambs --- pkging/info.go | 14 ++++++------- pkging/info_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ pkging/stdos/file.go | 2 +- 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 pkging/info_test.go diff --git a/pkging/info.go b/pkging/info.go index 8ec4e3b..418292d 100644 --- a/pkging/info.go +++ b/pkging/info.go @@ -65,23 +65,18 @@ func NewFileInfo(info os.FileInfo) *FileInfo { } func WithName(name string, info os.FileInfo) *FileInfo { - s := cleanName(name) - - if !strings.HasPrefix(s, "/") { - s = "/" + s - } - fo := NewFileInfo(info) fo.Details.Name = cleanName(name) return fo } func WithRelName(name string, info os.FileInfo) *FileInfo { + fo := NewFileInfo(info) + s := cleanName(name) s = strings.TrimPrefix(s, "/") - fo := NewFileInfo(info) - fo.Details.Name = cleanName(s) + fo.Details.Name = s return fo } @@ -89,5 +84,8 @@ func cleanName(s string) string { if strings.Contains(s, "\\") { s = strings.Replace(s, "\\", "/", -1) } + if !strings.HasPrefix(s, "/") { + s = "/" + s + } return s } diff --git a/pkging/info_test.go b/pkging/info_test.go new file mode 100644 index 0000000..bde8c0f --- /dev/null +++ b/pkging/info_test.go @@ -0,0 +1,47 @@ +package pkging + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_NewFileInfo(t *testing.T) { + const exp = "/public/images/mark.png" + + in := []string{ + "/public/images/mark.png", + "public/images/mark.png", + "/public\\images/mark.png", + "public/images\\mark.png", + "\\public\\images\\mark.png", + "public\\images\\mark.png", + "\\public/images\\mark.png", + "public\\images/mark.png", + "\\public\\images\\mark.png", + } + + for _, n := range in { + t.Run(n, func(st *testing.T) { + r := require.New(st) + + f1 := &FileInfo{ + Details: Details{ + Name: n, + Size: 42, + Mode: os.FileMode(0644), + IsDir: true, + }, + } + + f2 := NewFileInfo(f1) + + r.Equal(exp, f2.Name()) + r.Equal(f1.Size(), f2.Size()) + r.Equal(f1.Mode(), f2.Mode()) + r.Equal(f1.IsDir(), f2.IsDir()) + }) + } + +} diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 249c277..7ab1619 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -74,7 +74,7 @@ func (f *HTTP) Readdir(count int) ([]os.FileInfo, error) { infos := make([]os.FileInfo, len(osinfos)) for i, info := range osinfos { - infos[i] = pkging.WithName(info.Name(), info) + infos[i] = pkging.WithRelName(info.Name(), info) } return infos, err } From cad42281de18dbca418eb11bed8a2fb5efdd93f4 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 17:11:07 -0400 Subject: [PATCH 06/10] for you both --- pkging/info_test.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/pkging/info_test.go b/pkging/info_test.go index bde8c0f..4fe7afa 100644 --- a/pkging/info_test.go +++ b/pkging/info_test.go @@ -8,7 +8,6 @@ import ( ) func Test_NewFileInfo(t *testing.T) { - const exp = "/public/images/mark.png" in := []string{ "/public/images/mark.png", @@ -22,6 +21,7 @@ func Test_NewFileInfo(t *testing.T) { "\\public\\images\\mark.png", } + const exp = "/public/images/mark.png" for _, n := range in { t.Run(n, func(st *testing.T) { r := require.New(st) @@ -45,3 +45,38 @@ func Test_NewFileInfo(t *testing.T) { } } + +func Test_WithName(t *testing.T) { + + f1 := &FileInfo{ + Details: Details{ + Name: "/walls/crumbling", + Size: 42, + Mode: os.FileMode(0644), + IsDir: true, + }, + } + + const exp = "/public/images/mark.png" + in := []string{ + "/public/images/mark.png", + "public/images/mark.png", + "/public\\images/mark.png", + "public/images\\mark.png", + "\\public\\images\\mark.png", + "public\\images\\mark.png", + "\\public/images\\mark.png", + "public\\images/mark.png", + "\\public\\images\\mark.png", + } + + for _, n := range in { + t.Run(n, func(st *testing.T) { + r := require.New(st) + + f2 := WithName(n, f1) + + r.Equal(exp, f2.Name()) + }) + } +} From b28e301e65571d4b92b1d06c5d14600de8debd9e Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 17:22:59 -0400 Subject: [PATCH 07/10] party --- pkger.go | 89 ++++++++++++-------------------------------------- pkging/wrap.go | 8 +++++ 2 files changed, 28 insertions(+), 69 deletions(-) diff --git a/pkger.go b/pkger.go index 0a100da..2347997 100644 --- a/pkger.go +++ b/pkger.go @@ -1,6 +1,7 @@ package pkger import ( + "fmt" "log" "os" "path/filepath" @@ -18,117 +19,67 @@ var disk = func() pkging.Pkger { return n }() -func Parse(p string) (pkging.Path, error) { +func impl() pkging.Pkger { gil.RLock() defer gil.RUnlock() if current == nil { - return disk.Parse(p) + return disk } - return current.Parse(p) + fmt.Printf("!> using %s\n", current) + return current +} + +func Parse(p string) (pkging.Path, error) { + return impl().Parse(p) } func Abs(p string) (string, error) { - gil.RLock() - defer gil.RUnlock() - if current == nil { - return disk.Abs(p) - } - return current.Abs(p) + return impl().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) + return impl().AbsPath(p) } func Current() (here.Info, error) { - gil.RLock() - defer gil.RUnlock() - if current == nil { - return disk.Current() - } - return current.Current() + return impl().Current() } func Info(p string) (here.Info, error) { - gil.RLock() - defer gil.RUnlock() - if current == nil { - return disk.Info(p) - } - return current.Info(p) + return impl().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) + return impl().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) + return impl().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) + return impl().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) + return impl().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) + return impl().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) + return impl().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) + return impl().RemoveAll(name) } diff --git a/pkging/wrap.go b/pkging/wrap.go index 5c40519..20d0b5d 100644 --- a/pkging/wrap.go +++ b/pkging/wrap.go @@ -1,6 +1,7 @@ package pkging import ( + "fmt" "os" "path/filepath" @@ -19,6 +20,13 @@ type withPkger struct { parent Pkger } +func (w withPkger) String() string { + if w.parent == nil { + return fmt.Sprintf("%T", w.base) + } + return fmt.Sprintf("%T > %T", w.base, w.parent) +} + func (w withPkger) Parse(p string) (Path, error) { pt, err := w.base.Parse(p) if err != nil { From 158a7644cafcdead570b4c3da487552f2f49d5e2 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 17:38:03 -0400 Subject: [PATCH 08/10] clown strike --- apply.go | 6 +++++- pkging/pkgutil/dump.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pkging/pkgutil/dump.go diff --git a/apply.go b/apply.go index e90b1d8..c7e9160 100644 --- a/apply.go +++ b/apply.go @@ -1,16 +1,20 @@ package pkger import ( + "log" + "os" "sync" "github.com/markbates/pkger/pkging" + "github.com/markbates/pkger/pkging/pkgutil" ) var current pkging.Pkger var gil = &sync.RWMutex{} func Apply(pkg pkging.Pkger, err error) error { - if err != nil { + if err := pkgutil.Dump(os.Stdout, pkg); err != nil { + log.Fatal(err) return err } gil.Lock() diff --git a/pkging/pkgutil/dump.go b/pkging/pkgutil/dump.go new file mode 100644 index 0000000..fa214bd --- /dev/null +++ b/pkging/pkgutil/dump.go @@ -0,0 +1,46 @@ +package pkgutil + +import ( + "encoding/json" + "io" + "os" + + "github.com/markbates/pkger/here" + "github.com/markbates/pkger/pkging" +) + +func Dump(w io.Writer, pkg pkging.Pkger) error { + d := struct { + Info here.Info + Paths map[string]os.FileInfo + }{ + Paths: map[string]os.FileInfo{}, + } + + info, err := pkg.Current() + if err != nil { + return err + } + d.Info = info + + err = pkg.Walk("/", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + d.Paths[path] = info + return nil + }) + if err != nil { + return err + } + + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + + if err := enc.Encode(d); err != nil { + return err + } + + return nil +} From 53e641aa95cb8d6601890a0bb8718b71dbcb088e Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 17:52:07 -0400 Subject: [PATCH 09/10] release me --- pkger.go | 44 ++++++++++++++---------------------------- pkging/pkgutil/dump.go | 3 +++ 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/pkger.go b/pkger.go index 2347997..5a44b72 100644 --- a/pkger.go +++ b/pkger.go @@ -1,8 +1,6 @@ package pkger import ( - "fmt" - "log" "os" "path/filepath" @@ -11,75 +9,61 @@ import ( "github.com/markbates/pkger/pkging/stdos" ) -var disk = func() pkging.Pkger { - n, err := stdos.New() - if err != nil { - log.Println(err) - } - return n +var _ = func() error { + return Apply(stdos.New()) }() -func impl() pkging.Pkger { - gil.RLock() - defer gil.RUnlock() - if current == nil { - return disk - } - fmt.Printf("!> using %s\n", current) - return current -} - func Parse(p string) (pkging.Path, error) { - return impl().Parse(p) + return current.Parse(p) } func Abs(p string) (string, error) { - return impl().Abs(p) + return current.Abs(p) } func AbsPath(p pkging.Path) (string, error) { - return impl().AbsPath(p) + return current.AbsPath(p) } func Current() (here.Info, error) { - return impl().Current() + return current.Current() } func Info(p string) (here.Info, error) { - return impl().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) { - return impl().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 { - return impl().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) { - return impl().Open(p) + return current.Open(p) } // Stat returns a FileInfo describing the named file. func Stat(name string) (os.FileInfo, error) { - return impl().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 { - return impl().Walk(p, wf) + return current.Walk(p, wf) } // Remove removes the named file or (empty) directory. func Remove(name string) error { - return impl().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 { - return impl().RemoveAll(name) + return current.RemoveAll(name) } diff --git a/pkging/pkgutil/dump.go b/pkging/pkgutil/dump.go index fa214bd..128161a 100644 --- a/pkging/pkgutil/dump.go +++ b/pkging/pkgutil/dump.go @@ -2,6 +2,7 @@ package pkgutil import ( "encoding/json" + "fmt" "io" "os" @@ -11,9 +12,11 @@ import ( func Dump(w io.Writer, pkg pkging.Pkger) error { d := struct { + Type string Info here.Info Paths map[string]os.FileInfo }{ + Type: fmt.Sprintf("%T", pkg), Paths: map[string]os.FileInfo{}, } From 14453cbd80232c5cef25c23a0a08777db1b10f15 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 18:00:53 -0400 Subject: [PATCH 10/10] thats what i get --- apply.go | 6 ------ apply_test.go | 1 + pkger_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 apply_test.go create mode 100644 pkger_test.go diff --git a/apply.go b/apply.go index c7e9160..de3fe6f 100644 --- a/apply.go +++ b/apply.go @@ -1,24 +1,18 @@ package pkger import ( - "log" "os" - "sync" "github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging/pkgutil" ) var current pkging.Pkger -var gil = &sync.RWMutex{} func Apply(pkg pkging.Pkger, err error) error { if err := pkgutil.Dump(os.Stdout, pkg); err != nil { - log.Fatal(err) return err } - gil.Lock() - defer gil.Unlock() current = pkging.Wrap(current, pkg) return nil } diff --git a/apply_test.go b/apply_test.go new file mode 100644 index 0000000..df6d462 --- /dev/null +++ b/apply_test.go @@ -0,0 +1 @@ +package pkger diff --git a/pkger_test.go b/pkger_test.go new file mode 100644 index 0000000..53a717d --- /dev/null +++ b/pkger_test.go @@ -0,0 +1,44 @@ +package pkger + +import ( + "os" + "path/filepath" + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func Test_Parse(t *testing.T) { + r := require.New(t) + + pt, err := Parse("github.com/rocket/ship:/little") + r.NoError(err) + r.Equal("github.com/rocket/ship", pt.Pkg) + r.Equal("/little", pt.Name) +} + +func Test_Abs(t *testing.T) { + r := require.New(t) + + s, err := Abs(":/rocket.ship") + r.NoError(err) + + pwd, err := os.Getwd() + r.NoError(err) + r.Equal(filepath.Join(pwd, "rocket.ship"), s) +} + +func Test_AbsPath(t *testing.T) { + r := require.New(t) + + s, err := AbsPath(pkging.Path{ + Pkg: "github.com/markbates/pkger", + Name: "/rocket.ship", + }) + r.NoError(err) + + pwd, err := os.Getwd() + r.NoError(err) + r.Equal(filepath.Join(pwd, "rocket.ship"), s) +}