diff --git a/examples/app/go.mod b/examples/app/go.mod index 0521468..184f927 100644 --- a/examples/app/go.mod +++ b/examples/app/go.mod @@ -2,4 +2,6 @@ module github.com/markbates/pkger/examples/app go 1.12 -require github.com/markbates/pkger v0.0.0-20190830024022-c5e3a7de4d41 +require github.com/markbates/pkger v0.0.0 + +replace github.com/markbates/pkger => ../../ diff --git a/examples/app/go.sum b/examples/app/go.sum index 9cf6194..d3cbc9f 100644 --- a/examples/app/go.sum +++ b/examples/app/go.sum @@ -10,6 +10,7 @@ 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/markbates/pkger v0.0.0-20190830024022-c5e3a7de4d41 h1:bFEHpLBby7Qdvq92qw0TudeOfUKIZLcrweE/MMlNueI= github.com/markbates/pkger v0.0.0-20190830024022-c5e3a7de4d41/go.mod h1:M9VeozwduQUCr6z54kJrK9JegpbOv4wiePSbgSbFOew= +github.com/markbates/pkger/examples/app v0.0.0-20190830175641-68f3a2a4571d/go.mod h1:cWIt8Tz1UjmFa2KRaVgTfWPfHP+gprnzxDxNpo4YDxo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/examples/app/main.go b/examples/app/main.go index 28083e4..ef57b23 100644 --- a/examples/app/main.go +++ b/examples/app/main.go @@ -18,11 +18,7 @@ func main() { } defer pub.Close() - fi, err := pub.Stat() - if err != nil { - log.Fatal(err) - } - fmt.Println(fi) + fmt.Println(pub.Path()) mux.Handle("/t", http.StripPrefix("/t", tmplHandler())) mux.Handle("/", http.FileServer(pub)) diff --git a/go.mod b/go.mod index ef4f883..086da1b 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,8 @@ module github.com/markbates/pkger -go 1.12 +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/pkging/pkgtest/http.go b/pkging/pkgtest/http.go index b4d5c13..f0a88b4 100644 --- a/pkging/pkgtest/http.go +++ b/pkging/pkgtest/http.go @@ -135,48 +135,48 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) { } } -// func (s Suite) Test_HTTP_File(t *testing.T) { -// r := require.New(t) -// -// cur, err := s.Current() -// r.NoError(err) -// ip := cur.ImportPath -// -// table := []struct { -// in string -// }{ -// {in: "/public"}, -// {in: ":" + "/public"}, -// {in: ip + ":" + "/public"}, -// } -// -// for _, tt := range table { -// s.Run(t, tt.in, func(st *testing.T) { -// -// r := require.New(st) -// -// r.NoError(s.LoadFolder()) -// -// dir, err := s.Open(tt.in) -// r.NoError(err) -// defer dir.Close() -// -// ts := httptest.NewServer(http.FileServer(dir)) -// defer ts.Close() -// -// res, err := http.Get(ts.URL + "/images/mark.png") -// r.NoError(err) -// r.Equal(200, res.StatusCode) -// -// b, err := ioutil.ReadAll(res.Body) -// r.NoError(err) -// -// body := string(b) -// r.Contains(body, `!/public/images/mark.png`) -// }) -// } -// -// } +func (s Suite) Test_HTTP_File(t *testing.T) { + r := require.New(t) + + cur, err := s.Current() + r.NoError(err) + ip := cur.ImportPath + + table := []struct { + in string + }{ + {in: "/public"}, + {in: ":" + "/public"}, + {in: ip + ":" + "/public"}, + } + + for _, tt := range table { + s.Run(t, tt.in, func(st *testing.T) { + + r := require.New(st) + + r.NoError(s.LoadFolder()) + + dir, err := s.Open(tt.in) + r.NoError(err) + defer dir.Close() + + ts := httptest.NewServer(http.FileServer(dir)) + defer ts.Close() + + res, err := http.Get(ts.URL + "/images/mark.png") + r.NoError(err) + r.Equal(200, res.StatusCode) + + b, err := ioutil.ReadAll(res.Body) + r.NoError(err) + + body := string(b) + r.Contains(body, `!/public/images/mark.png`) + }) + } + +} // // func (s Suite) Test_HTTP_Dir_Memory_StripPrefix(t *testing.T) { diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 21dc58f..303e697 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -3,6 +3,7 @@ package stdos import ( "net/http" "os" + "path" "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" @@ -60,8 +61,31 @@ func (f *File) Name() string { return f.info.Name() } +type HTTP struct { + pkging.File + osf *os.File +} + +func (f *HTTP) Readdir(count int) ([]os.FileInfo, error) { + osinfos, err := f.osf.Readdir(count) + if err != nil { + return nil, err + } + + infos := make([]os.FileInfo, len(osinfos)) + for i, info := range osinfos { + infos[i] = pkging.WithName(info.Name(), info) + } + return infos, err +} + func (f *File) Open(name string) (http.File, error) { - return f.File, nil + fp := path.Join(f.Path().Name, name) + f2, err := f.pkging.Open(fp) + if err != nil { + return nil, err + } + return &HTTP{File: f2, osf: f.File}, nil } func (f *File) Path() pkging.Path {