the wheel

This commit is contained in:
Mark Bates 2019-10-25 09:30:31 -04:00
parent 1154513c57
commit a1787a9901
6 changed files with 13 additions and 101 deletions

View File

@ -1,5 +1,4 @@
default: default:
go get github.com/markbates/pkger/cmd/pkger
pkger pkger
GOOS=linux go build -v -o example GOOS=linux go build -v -o example
docker build -t pkger:example . docker build -t pkger:example .

View File

@ -14,9 +14,6 @@ func main() {
} }
func run() error { func run() error {
dir, err := pkger.HTTP("/public") dir := http.FileServer(pkger.Dir("/public"))
if err != nil {
return err
}
return http.ListenAndServe(":3000", dir) return http.ListenAndServe(":3000", dir)
} }

View File

@ -21,7 +21,7 @@ func (d HTTPDecl) String() string {
func (d HTTPDecl) MarshalJSON() ([]byte, error) { func (d HTTPDecl) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"type": "pkger.HTTP", "type": "pkger.Dir",
"file": d.file, "file": d.file,
"pos": d.pos, "pos": d.pos,
"value": d.value, "value": d.value,

View File

@ -286,7 +286,7 @@ func (f *file) findHTTPCalls() error {
return true return true
} }
exists := isPkgDot(ce.Fun, "pkger", "HTTP") exists := isPkgDot(ce.Fun, "pkger", "Dir")
if !(exists) || len(ce.Args) != 1 { if !(exists) || len(ce.Args) != 1 {
return true return true
} }

View File

@ -36,6 +36,16 @@ func impl() pkging.Pkger {
return current 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. // Parse the string in here.Path format.
func Parse(p string) (here.Path, error) { func Parse(p string) (here.Path, error) {
return impl().Parse(p) return impl().Parse(p)
@ -95,12 +105,3 @@ func Remove(name string) error {
func RemoveAll(name string) error { func RemoveAll(name string) error {
return impl().RemoveAll(name) 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
}

View File

@ -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)
}