From a1787a9901167a24afeaf8d49a6a4951271d49db Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 25 Oct 2019 09:30:31 -0400 Subject: [PATCH] the wheel --- examples/http/pkger/Makefile | 1 - examples/http/pkger/main.go | 5 +-- parser/http.go | 2 +- parser/visitor.go | 2 +- pkger.go | 19 ++++---- pkging/mem/embed_test.go | 85 ------------------------------------ 6 files changed, 13 insertions(+), 101 deletions(-) delete mode 100644 pkging/mem/embed_test.go diff --git a/examples/http/pkger/Makefile b/examples/http/pkger/Makefile index d12579d..dd14cf3 100644 --- a/examples/http/pkger/Makefile +++ b/examples/http/pkger/Makefile @@ -1,5 +1,4 @@ default: - go get github.com/markbates/pkger/cmd/pkger pkger GOOS=linux go build -v -o example docker build -t pkger:example . diff --git a/examples/http/pkger/main.go b/examples/http/pkger/main.go index dcb75e2..0552933 100644 --- a/examples/http/pkger/main.go +++ b/examples/http/pkger/main.go @@ -14,9 +14,6 @@ func main() { } func run() error { - dir, err := pkger.HTTP("/public") - if err != nil { - return err - } + dir := http.FileServer(pkger.Dir("/public")) return http.ListenAndServe(":3000", dir) } diff --git a/parser/http.go b/parser/http.go index 893e151..360dead 100644 --- a/parser/http.go +++ b/parser/http.go @@ -21,7 +21,7 @@ func (d HTTPDecl) String() string { func (d HTTPDecl) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]interface{}{ - "type": "pkger.HTTP", + "type": "pkger.Dir", "file": d.file, "pos": d.pos, "value": d.value, diff --git a/parser/visitor.go b/parser/visitor.go index 13a6f78..88dae74 100644 --- a/parser/visitor.go +++ b/parser/visitor.go @@ -286,7 +286,7 @@ func (f *file) findHTTPCalls() error { return true } - exists := isPkgDot(ce.Fun, "pkger", "HTTP") + exists := isPkgDot(ce.Fun, "pkger", "Dir") if !(exists) || len(ce.Args) != 1 { return true } diff --git a/pkger.go b/pkger.go index 62cf39c..6bcbd27 100644 --- a/pkger.go +++ b/pkger.go @@ -36,6 +36,16 @@ func impl() pkging.Pkger { return current } +type Dir string + +func (d Dir) Open(name string) (http.File, error) { + f, err := Open(string(d)) + if err != nil { + return nil, err + } + return f.Open(name) +} + // Parse the string in here.Path format. func Parse(p string) (here.Path, error) { return impl().Parse(p) @@ -95,12 +105,3 @@ func Remove(name string) error { func RemoveAll(name string) error { return impl().RemoveAll(name) } - -// HTTP returns an http.FileServer for the specified path. -func HTTP(p string) (http.Handler, error) { - f, err := Open(p) - if err != nil { - return nil, err - } - return http.FileServer(f), nil -} diff --git a/pkging/mem/embed_test.go b/pkging/mem/embed_test.go deleted file mode 100644 index acb4790..0000000 --- a/pkging/mem/embed_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package mem_test - -import ( - "bytes" - "os" - "path/filepath" - "testing" - - "github.com/markbates/pkger/parser" - "github.com/markbates/pkger/pkging/mem" - "github.com/markbates/pkger/pkging/pkgtest" - "github.com/markbates/pkger/pkging/pkgutil" - "github.com/stretchr/testify/require" -) - -func Test_Pkger_Embedding(t *testing.T) { - r := require.New(t) - - app, err := pkgtest.App() - r.NoError(err) - - res, err := parser.Parse(app.Info) - r.NoError(err) - - files, err := res.Files() - r.NoError(err) - - ps := make([]string, len(files)) - for i, f := range files { - ps[i] = f.Path.String() - } - - r.Equal(app.Paths.Parser, ps) - - base, err := mem.New(app.Info) - r.NoError(err) - - err = filepath.Walk(app.Info.Dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - f, err := os.Open(path) - if err != nil { - return err - } - defer f.Close() - return base.Add(f) - - }) - r.NoError(err) - - act := []string{} - err = base.Walk("/public", func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - act = append(act, path) - return nil - }) - - r.NoError(err) - r.Equal(app.Paths.Public, act) - - bb := &bytes.Buffer{} - - err = pkgutil.Stuff(bb, app.Info, res) - r.NoError(err) - - pkg := &mem.Pkger{} - err = pkg.UnmarshalEmbed(bb.Bytes()) - r.NoError(err) - - act = []string{} - err = pkg.Walk("/public", func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - act = append(act, path) - return nil - }) - - r.NoError(err) - r.Equal(app.Paths.Public, act) -}