diff --git a/cmd/pkger/pack.go b/cmd/pkger/pack.go index 4a10f46..ab860d5 100644 --- a/cmd/pkger/pack.go +++ b/cmd/pkger/pack.go @@ -7,8 +7,8 @@ import ( "sort" "github.com/markbates/pkger" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/parser" - "github.com/markbates/pkger/pkging" "github.com/markbates/pkger/stuffing" ) @@ -30,16 +30,16 @@ func (e *packCmd) Exec(args []string) error { if err != nil { return err } - + fmt.Println(info) res, err := parser.Parse(info) if err != nil { return err } if e.list { - fmt.Println(res.Path) + fmt.Println(info.ImportPath) - for _, p := range res.Paths { + for _, p := range res { fmt.Printf(" > %s\n", p) } return nil @@ -48,7 +48,7 @@ func (e *packCmd) Exec(args []string) error { fp := info.FilePath(outName) os.RemoveAll(fp) - if err := Package(fp, res.Paths); err != nil { + if err := Package(fp, res); err != nil { return err } @@ -118,7 +118,7 @@ func (e *packCmd) Flags() *flag.FlagSet { return e.FlagSet } -func Package(out string, paths []pkging.Path) error { +func Package(out string, paths []here.Path) error { os.RemoveAll(out) f, err := os.Create(out) diff --git a/examples/app/go.sum b/examples/app/go.sum index eebdbae..a4282e2 100644 --- a/examples/app/go.sum +++ b/examples/app/go.sum @@ -588,6 +588,8 @@ github.com/markbates/grift v1.0.5/go.mod h1:EHmVIjOQoj/OOBDzlZ8RW0ZkvOtQ4xRHjrPv github.com/markbates/grift v1.0.6/go.mod h1:2AUYA/+pODhwonRbYwsltPVPIztBzw5nIJEGiWgKMPM= github.com/markbates/grift v1.1.0 h1:DsljFKUSK1ELpU22ZE+Gi93jiQI3cYD/RQ+vHM/PpY8= github.com/markbates/grift v1.1.0/go.mod h1:8N7ybWEcnMOvtSb0kW+dLJpYii9eq/FP3Gtu/cNPDTY= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b h1:ns0oO2sMEoFJMmrbiWzGQO5AR3GgqfYRAos0gz8C0Cw= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b/go.mod h1:jHlCX3RNqF+epcY1FxjLyDGzr3l9+mNCh3YDDw6BFvY= github.com/markbates/hmax v1.0.0/go.mod h1:cOkR9dktiESxIMu+65oc/r/bdY4bE8zZw3OLhLx0X2c= github.com/markbates/hmax v1.1.0/go.mod h1:hhn8pJiRwNTEmNlxhfiTbL+CtEYiAX3wuhSf/kg/6wI= github.com/markbates/inflect v1.0.0/go.mod h1:oTeZL2KHA7CUX6X+fovmK9OvIOFuqu0TwdQrZjLTh88= diff --git a/examples/app/main.go b/examples/app/main.go index b1c9515..a318e8a 100644 --- a/examples/app/main.go +++ b/examples/app/main.go @@ -22,10 +22,11 @@ func main() { if err != nil { log.Fatal(err) } + fmt.Println(current) fmt.Printf("Walking files for %s\n", current.ImportPath) // walk the files in this module. "/" is where the `go.mod` for this module is - err = pkger.Walk("/", func(path string, info os.FileInfo, err error) error { + err = pkger.Walk("github.com/gobuffalo/buffalo:/", func(path string, info os.FileInfo, err error) error { if err != nil { return err } diff --git a/examples/complex/.gitignore b/examples/complex/.gitignore new file mode 100644 index 0000000..ef4215f --- /dev/null +++ b/examples/complex/.gitignore @@ -0,0 +1,3 @@ +example +app +pkged.go diff --git a/examples/complex/Dockerfile b/examples/complex/Dockerfile new file mode 100644 index 0000000..1360374 --- /dev/null +++ b/examples/complex/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine + +EXPOSE 3000 +COPY example /bin/ +RUN ls -la + +CMD /bin/example diff --git a/examples/complex/Makefile b/examples/complex/Makefile new file mode 100644 index 0000000..3b0e05a --- /dev/null +++ b/examples/complex/Makefile @@ -0,0 +1,7 @@ +default: + cd ../../cmd/pkger && go install -v . + pkger -list + pkger + GOOS=linux go build -v -o example + docker build -t pkger:example . + docker run -p 3000:3000 pkger:example diff --git a/examples/complex/api/api.go b/examples/complex/api/api.go new file mode 100644 index 0000000..f5b4359 --- /dev/null +++ b/examples/complex/api/api.go @@ -0,0 +1,17 @@ +package api + +import ( + "io/ioutil" + + "github.com/markbates/pkger" +) + +func Version() (string, error) { + f, err := pkger.Open("github.com/markbates/pkger/examples/complex/api:/version.txt") + if err != nil { + return "", err + } + defer f.Close() + b, err := ioutil.ReadAll(f) + return string(b), err +} diff --git a/examples/complex/api/go.mod b/examples/complex/api/go.mod new file mode 100644 index 0000000..2db308b --- /dev/null +++ b/examples/complex/api/go.mod @@ -0,0 +1,7 @@ +module github.com/markbates/pkger/examples/complex/api + +go 1.13 + +require github.com/markbates/pkger v0.0.0 + +replace github.com/markbates/pkger => ../../../ diff --git a/examples/complex/api/go.sum b/examples/complex/api/go.sum new file mode 100644 index 0000000..6926bde --- /dev/null +++ b/examples/complex/api/go.sum @@ -0,0 +1,16 @@ +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/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= +github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI= +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= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +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/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/examples/complex/api/version.txt b/examples/complex/api/version.txt new file mode 100644 index 0000000..0ec25f7 --- /dev/null +++ b/examples/complex/api/version.txt @@ -0,0 +1 @@ +v1.0.0 diff --git a/examples/complex/go.mod b/examples/complex/go.mod new file mode 100644 index 0000000..7647bdf --- /dev/null +++ b/examples/complex/go.mod @@ -0,0 +1,12 @@ +module github.com/markbates/pkger/examples/complex + +go 1.13 + +require ( + github.com/markbates/pkger v0.0.0 + github.com/markbates/pkger/examples/complex/api v0.0.0 +) + +replace github.com/markbates/pkger => ../../ + +replace github.com/markbates/pkger/examples/complex/api => ./api diff --git a/examples/complex/go.sum b/examples/complex/go.sum new file mode 100644 index 0000000..751570d --- /dev/null +++ b/examples/complex/go.sum @@ -0,0 +1,23 @@ +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/markbates/hepa v0.0.0-20190718154049-1d900199db5b h1:ns0oO2sMEoFJMmrbiWzGQO5AR3GgqfYRAos0gz8C0Cw= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b/go.mod h1:jHlCX3RNqF+epcY1FxjLyDGzr3l9+mNCh3YDDw6BFvY= +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= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +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/examples/complex/main.go b/examples/complex/main.go new file mode 100644 index 0000000..941fbcc --- /dev/null +++ b/examples/complex/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "log" + + "github.com/markbates/pkger/examples/complex/api" +) + +const host = ":3000" + +func main() { + v, err := api.Version() + if err != nil { + log.Fatal(err) + } + fmt.Println("Version: ", v) + + if err := writeFile(); err != nil { + log.Fatal(err) + } + + if err := readFile(); err != nil { + log.Fatal(err) + } +} diff --git a/examples/complex/read.go b/examples/complex/read.go new file mode 100644 index 0000000..ea345d0 --- /dev/null +++ b/examples/complex/read.go @@ -0,0 +1,18 @@ +package main + +import ( + "io" + "os" + + "github.com/markbates/pkger" +) + +func readFile() error { + f, err := pkger.Open("/go.mod") + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(os.Stdout, f) + return err +} diff --git a/examples/complex/write.go b/examples/complex/write.go new file mode 100644 index 0000000..4b6d703 --- /dev/null +++ b/examples/complex/write.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + + "github.com/markbates/pkger" +) + +// writeFile will write *REAL* files when +// not packaged. When packaged, writeFile +// will write into memory instead. +func writeFile() error { + // make a folder structure to write into. + // just like the `os` package directory + // structures are *NOT* created for you. + if err := pkger.MkdirAll("/delta/88", 0755); err != nil { + return err + } + // remove the /delta folder and anything + // underneath it + defer pkger.RemoveAll("/delta") + + // create a new file under the `/delta/88` + // directory named `a.car`. + f, err := pkger.Create("/delta/88/a.car") + if err != nil { + return err + } + + msg := []byte("The Caravan Stops") + i, err := f.Write(msg) + if err != nil { + return err + } + if i != len(msg) { + return fmt.Errorf("expected to write %d bytes, wrote %d instead", len(msg), i) + } + + // close the file + if err := f.Close(); err != nil { + return err + } + + // stat the new file and get back its os.FileInfo + info, err := pkger.Stat("/delta/88/a.car") + if err != nil { + return err + } + + fmt.Println("info.Name()\t", info.Name()) + fmt.Println("info.Size()\t", info.Size()) + fmt.Println("info.Mode()\t", info.Mode()) + fmt.Println("info.IsDir()\t", info.IsDir()) + + return nil +} diff --git a/go.mod b/go.mod index 2687563..1185fde 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,10 @@ 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/markbates/oncer v1.0.0 + github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b 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 6ca866c..e800a39 100644 --- a/go.sum +++ b/go.sum @@ -2,17 +2,23 @@ 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/markbates/oncer v1.0.0 h1:E83IaVAHygyndzPimgUYJjbshhDTALZyXxvk9FOlQRY= -github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b h1:ns0oO2sMEoFJMmrbiWzGQO5AR3GgqfYRAos0gz8C0Cw= +github.com/markbates/hepa v0.0.0-20190718154049-1d900199db5b/go.mod h1:jHlCX3RNqF+epcY1FxjLyDGzr3l9+mNCh3YDDw6BFvY= 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= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 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/info.go b/here/info.go index 6b47969..ca64156 100644 --- a/here/info.go +++ b/here/info.go @@ -2,29 +2,51 @@ package here import ( "encoding/json" + "fmt" "os" "path/filepath" + "regexp" "runtime" "strings" + + "github.com/markbates/hepa" + "github.com/markbates/hepa/filters" ) // Info represents details about the directory/package type Info struct { - Dir string - ImportPath string - Name string - Doc string - Target string - Root string - Match []string - Stale bool - StaleReason string - GoFiles []string - Imports []string - Deps []string - TestGoFiles []string - TestImports []string - Module Module + Dir string + ImportPath string + Name string + Imports []string + Module Module +} + +func (fi Info) MarshalJSON() ([]byte, error) { + mm := map[string]interface{}{ + "ImportPath": fi.ImportPath, + "Name": fi.Name, + "Imports": fi.Imports, + "Module": fi.Module, + } + + hep := hepa.New() + hep = hepa.With(hep, filters.Home()) + hep = hepa.With(hep, filters.Golang()) + + cm := map[string]string{ + "Dir": fi.Dir, + } + + for k, v := range cm { + b, err := hep.Filter([]byte(v)) + if err != nil { + return nil, err + } + mm[k] = string(b) + } + + return json.Marshal(mm) } func (i Info) FilePath(paths ...string) string { @@ -74,3 +96,58 @@ func (i Info) String() string { s := string(b) return s } + +func (i Info) Parse(p string) (Path, error) { + p = strings.TrimSpace(p) + p = filepath.Clean(p) + p = strings.TrimPrefix(p, i.Dir) + + p = strings.Replace(p, "\\", "/", -1) + p = strings.TrimSpace(p) + + if len(p) == 0 || p == ":" { + return i.build("", "", "") + } + + res := pathrx.FindAllStringSubmatch(p, -1) + if len(res) == 0 { + return Path{}, fmt.Errorf("could not parse %q", p) + } + + matches := res[0] + + if len(matches) != 4 { + return Path{}, fmt.Errorf("could not parse %q", p) + } + + return i.build(p, matches[1], matches[3]) +} + +func (i Info) build(p, pkg, name string) (Path, error) { + pt := Path{ + Pkg: pkg, + Name: name, + } + + if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 { + pt.Name = pt.Pkg + pt.Pkg = i.ImportPath + } + + if len(pt.Name) == 0 { + pt.Name = "/" + } + + if pt.Pkg == pt.Name { + pt.Pkg = i.ImportPath + pt.Name = "/" + } + + if !strings.HasPrefix(pt.Name, "/") { + pt.Name = "/" + pt.Name + } + pt.Name = strings.TrimPrefix(pt.Name, i.Dir) + return pt, nil +} + +var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") diff --git a/here/module.go b/here/module.go index 50be1eb..324355d 100644 --- a/here/module.go +++ b/here/module.go @@ -1,13 +1,45 @@ package here -import "encoding/json" +import ( + "encoding/json" + + "github.com/markbates/hepa" + "github.com/markbates/hepa/filters" +) type Module struct { - Path string `json:"Path"` - Main bool `json:"Main"` - Dir string `json:"Dir"` - GoMod string `json:"GoMod"` - GoVersion string `json:"GoVersion"` + Path string + Main bool + Dir string + GoMod string + GoVersion string +} + +func (m Module) MarshalJSON() ([]byte, error) { + mm := map[string]interface{}{ + "Main": m.Main, + "GoVersion": m.GoVersion, + } + + hep := hepa.New() + hep = hepa.With(hep, filters.Home()) + hep = hepa.With(hep, filters.Golang()) + + cm := map[string]string{ + "Path": m.Path, + "Dir": m.Dir, + "GoMod": m.GoMod, + } + + for k, v := range cm { + b, err := hep.Filter([]byte(v)) + if err != nil { + return nil, err + } + mm[k] = string(b) + } + + return json.Marshal(mm) } func (i Module) String() string { diff --git a/pkging/path.go b/here/path.go similarity index 97% rename from pkging/path.go rename to here/path.go index f09061c..2b8b90c 100644 --- a/pkging/path.go +++ b/here/path.go @@ -1,4 +1,4 @@ -package pkging +package here import ( "encoding/json" diff --git a/internal/maps/files.go b/internal/maps/files.go index bf5ab3a..3b3b6ac 100644 --- a/internal/maps/files.go +++ b/internal/maps/files.go @@ -8,11 +8,12 @@ import ( "sort" "sync" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" ) // Files wraps sync.Map and uses the following types: -// key: pkging.Path +// key: here.Path // value: pkging.File type Files struct { data *sync.Map @@ -58,7 +59,7 @@ func (m *Files) UnmarshalJSON(b []byte) error { return err } for k, v := range mm { - var pt pkging.Path + var pt here.Path if err := json.Unmarshal([]byte(k), &pt); err != nil { return err } @@ -68,7 +69,7 @@ func (m *Files) UnmarshalJSON(b []byte) error { } // Delete the key from the map -func (m *Files) Delete(key pkging.Path) { +func (m *Files) Delete(key here.Path) { m.Data().Delete(key) } @@ -76,7 +77,7 @@ func (m *Files) Delete(key pkging.Path) { // Returns pkging.File or bool. // A false return indicates either the key was not found // or the value is not of type pkging.File -func (m *Files) Load(key pkging.Path) (pkging.File, bool) { +func (m *Files) Load(key here.Path) (pkging.File, bool) { i, ok := m.Data().Load(key) if !ok { return nil, false @@ -86,9 +87,9 @@ func (m *Files) Load(key pkging.Path) (pkging.File, bool) { } // Range over the pkging.File values in the map -func (m *Files) Range(f func(key pkging.Path, value pkging.File) bool) { +func (m *Files) Range(f func(key here.Path, value pkging.File) bool) { m.Data().Range(func(k, v interface{}) bool { - key, ok := k.(pkging.Path) + key, ok := k.(here.Path) if !ok { return false } @@ -101,14 +102,14 @@ func (m *Files) Range(f func(key pkging.Path, value pkging.File) bool) { } // Store a pkging.File in the map -func (m *Files) Store(key pkging.Path, value pkging.File) { +func (m *Files) Store(key here.Path, value pkging.File) { m.Data().Store(key, value) } // Keys returns a list of keys in the map -func (m *Files) Keys() []pkging.Path { - var keys []pkging.Path - m.Range(func(key pkging.Path, value pkging.File) bool { +func (m *Files) Keys() []here.Path { + var keys []here.Path + m.Range(func(key here.Path, value pkging.File) bool { keys = append(keys, key) return true }) diff --git a/internal/maps/paths.go b/internal/maps/paths.go deleted file mode 100644 index 3dff246..0000000 --- a/internal/maps/paths.go +++ /dev/null @@ -1,165 +0,0 @@ -package maps - -import ( - "encoding/json" - "fmt" - "path/filepath" - "regexp" - "sort" - "strings" - "sync" - - "github.com/markbates/pkger/here" - "github.com/markbates/pkger/pkging" -) - -// Paths wraps sync.Map and uses the following types: -// key: string -// value: Path -type Paths struct { - Current here.Info - data *sync.Map - once *sync.Once -} - -func (m *Paths) Data() *sync.Map { - if m.once == nil { - m.once = &sync.Once{} - } - m.once.Do(func() { - if m.data == nil { - m.data = &sync.Map{} - } - }) - return m.data -} - -func (m *Paths) MarshalJSON() ([]byte, error) { - mm := map[string]interface{}{} - m.Data().Range(func(key, value interface{}) bool { - mm[fmt.Sprintf("%s", key)] = value - return true - }) - return json.Marshal(mm) -} - -func (m *Paths) UnmarshalJSON(b []byte) error { - mm := map[string]pkging.Path{} - - if err := json.Unmarshal(b, &mm); err != nil { - return err - } - for k, v := range mm { - m.Store(k, v) - } - return nil -} - -// Delete the key from the map -func (m *Paths) Delete(key string) { - m.Data().Delete(key) -} - -// Load the key from the map. -// Returns Path or bool. -// A false return indicates either the key was not found -// or the value is not of type Path -func (m *Paths) Load(key string) (pkging.Path, bool) { - i, ok := m.Data().Load(key) - if !ok { - return pkging.Path{}, false - } - s, ok := i.(pkging.Path) - return s, ok -} - -// Range over the Path values in the map -func (m *Paths) Range(f func(key string, value pkging.Path) bool) { - m.Data().Range(func(k, v interface{}) bool { - key, ok := k.(string) - if !ok { - return false - } - value, ok := v.(pkging.Path) - if !ok { - return false - } - return f(key, value) - }) -} - -// Store a Path in the map -func (m *Paths) Store(key string, value pkging.Path) { - m.Data().Store(key, value) -} - -// Keys returns a list of keys in the map -func (m *Paths) Keys() []string { - var keys []string - m.Range(func(key string, value pkging.Path) bool { - keys = append(keys, key) - return true - }) - sort.Strings(keys) - return keys -} - -func (m *Paths) Parse(p string) (pkging.Path, error) { - p = strings.TrimSpace(p) - p = filepath.Clean(p) - p = strings.TrimPrefix(p, m.Current.Dir) - - p = strings.Replace(p, "\\", "/", -1) - p = strings.TrimSpace(p) - - pt, ok := m.Load(p) - if ok { - return pt, nil - } - if len(p) == 0 || p == ":" { - return m.build("", "", "") - } - - res := pathrx.FindAllStringSubmatch(p, -1) - if len(res) == 0 { - return pt, fmt.Errorf("could not parse %q", p) - } - - matches := res[0] - - if len(matches) != 4 { - return pt, fmt.Errorf("could not parse %q", p) - } - - return m.build(p, matches[1], matches[3]) -} - -var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") - -func (m *Paths) build(p, pkg, name string) (pkging.Path, error) { - pt := pkging.Path{ - Pkg: pkg, - Name: name, - } - - if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 { - pt.Name = pt.Pkg - pt.Pkg = m.Current.ImportPath - } - - if len(pt.Name) == 0 { - pt.Name = "/" - } - - if pt.Pkg == pt.Name { - pt.Pkg = m.Current.ImportPath - pt.Name = "/" - } - - if !strings.HasPrefix(pt.Name, "/") { - pt.Name = "/" + pt.Name - } - pt.Name = strings.TrimPrefix(pt.Name, m.Current.Dir) - m.Store(p, pt) - return pt, nil -} diff --git a/parser/parser.go b/parser/parser.go index f0a5c98..3098833 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -5,46 +5,25 @@ import ( "go/parser" "go/token" "os" + "sort" - "github.com/markbates/oncer" + "github.com/markbates/pkger" "github.com/markbates/pkger/here" - "github.com/markbates/pkger/pkging" ) var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"} -func Parse(her here.Info) (Results, error) { - var r Results - var err error +func Parse(her here.Info) ([]here.Path, error) { - oncer.Do(her.ImportPath, func() { - pwd, err := os.Getwd() - if err != nil { - return - } - defer os.Chdir(pwd) + src, err := fromSource(her) + if err != nil { + return nil, err + } - fmt.Println("cd: ", her.Dir, her.ImportPath) - os.Chdir(her.Dir) - - // 1. search for .go files in/imported by `her.ImportPath` - src, err := fromSource(her) - if err != nil { - return - } - fmt.Println(">>>TODO parser/parser.go:30: src ", src) - - // 2. parse .go ast's for `pkger.*` calls - // 3. find path's in those files - // 4. walk folders in those paths and add to results - }) - - return r, err + return src, nil } -func fromSource(her here.Info) ([]pkging.Path, error) { - fmt.Println(">>>TODO parser/parser.go:201: her.ImportPath ", her.ImportPath) - fmt.Println(her) +func fromSource(her here.Info) ([]here.Path, error) { root := her.Dir fi, err := os.Stat(root) if err != nil { @@ -60,11 +39,16 @@ func fromSource(her here.Info) ([]pkging.Path, error) { if err != nil { return nil, err } - - var paths []pkging.Path + pm := map[string]here.Path{} for _, pkg := range pkgs { for _, pf := range pkg.Files { - f := &file{fset: fset, astFile: pf, filename: pf.Name.Name} + f := &file{ + info: her, + fset: fset, + astFile: pf, + filename: pf.Name.Name, + paths: map[string]here.Path{}, + } f.decls = make(map[string]string) x, err := f.find() @@ -76,15 +60,46 @@ func fromSource(her here.Info) ([]pkging.Path, error) { pt.Pkg = her.ImportPath x[i] = pt } - paths = append(paths, x[i]) + pt = x[i] + pm[pt.String()] = pt + err = pkger.Walk(pt.String(), func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + p, err := her.Parse(path) + if err != nil { + return err + } + + if pt.Name != "/" { + p.Name = pt.Name + p.Name + } + pm[p.String()] = p + return nil + }) + if err != nil { + return nil, err + } } } } + var paths []here.Path - for _, i := range her.Imports { - fmt.Println(">>>TODO parser/parser.go:237: i ", i) + for _, v := range pm { + paths = append(paths, v) } + sort.Slice(paths, func(i, j int) bool { + return paths[i].String() < paths[j].String() + }) + + // for _, i := range her.Imports { + // fmt.Println(">>>TODO parser/parser.go:237: i ", i) + // } + return paths, nil } @@ -127,6 +142,6 @@ func fromSource(her here.Info) ([]pkging.Path, error) { // } type Results struct { - Paths []pkging.Path - Path pkging.Path + Paths []here.Path + Path here.Path } diff --git a/parser/parser_test.go b/parser/parser_test.go index 60c2d08..2629c8f 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -18,10 +18,10 @@ func Test_Parser(t *testing.T) { ch := filepath.Join(pwd, "..", "examples", - "app") + "complex") info := here.Info{ Dir: ch, - ImportPath: "github.com/markbates/pkger/examples/app", + ImportPath: "github.com/markbates/pkger/examples/complex", } res, err := Parse(info) @@ -29,19 +29,20 @@ func Test_Parser(t *testing.T) { r.NoError(err) exp := []string{ - "github.com/markbates/pkger/examples/app:/", - "github.com/markbates/pkger/examples/app:/public", - "github.com/markbates/pkger/examples/app:/public/images/mark-small.png", - "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/complex:/", + "github.com/markbates/pkger/examples/complex:/go.mod", + // "github.com/markbates/pkger/examples/app:/public", + // "github.com/markbates/pkger/examples/app:/public/images/mark-small.png", + // "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", } sort.Strings(exp) - act := make([]string, len(res.Paths)) - for i := 0; i < len(res.Paths); i++ { - act[i] = res.Paths[i].String() + act := make([]string, len(res)) + for i := 0; i < len(res); i++ { + act[i] = res[i].String() } sort.Strings(act) diff --git a/parser/visitor.go b/parser/visitor.go index b417473..fbc2dcf 100644 --- a/parser/visitor.go +++ b/parser/visitor.go @@ -4,11 +4,10 @@ import ( "fmt" "go/ast" "go/token" + "sort" "strconv" - "github.com/markbates/pkger" "github.com/markbates/pkger/here" - "github.com/markbates/pkger/pkging" ) type visitor func(node ast.Node) (w ast.Visitor) @@ -19,18 +18,20 @@ func (v visitor) Visit(node ast.Node) ast.Visitor { // inspired by https://gist.github.com/cryptix/d1b129361cea51a59af2 type file struct { + info here.Info fset *token.FileSet astFile *ast.File filename string decls map[string]string - paths []pkging.Path + paths map[string]here.Path } func (f *file) walk(fn func(ast.Node) bool) { ast.Walk(walker(fn), f.astFile) } -func (f *file) find() ([]pkging.Path, error) { +func (f *file) find() ([]here.Path, error) { + f.paths = map[string]here.Path{} if err := f.findDecals(); err != nil { return nil, err } @@ -46,7 +47,14 @@ func (f *file) find() ([]pkging.Path, error) { return nil, err } - return f.paths, nil + var paths []here.Path + for _, p := range f.paths { + paths = append(paths, p) + } + sort.Slice(paths, func(a, b int) bool { + return paths[a].String() < paths[b].String() + }) + return paths, nil } func (f *file) findDecals() error { @@ -113,12 +121,13 @@ func (f *file) findOpenCalls() error { err = nil return false } - pt, err := pkger.Parse(s) + pt, err := f.info.Parse(s) + fmt.Println(">>>TODO parser/visitor.go:124: pt ", pt) if err != nil { err = err return false } - f.paths = append(f.paths, pt) + f.paths[pt.String()] = pt case *ast.Ident: val, ok := f.decls[x.Name] if !ok { @@ -131,12 +140,13 @@ func (f *file) findOpenCalls() error { err = nil return false } - pt, err := pkger.Parse(s) + pt, err := f.info.Parse(s) + fmt.Println(">>>TODO parser/visitor.go:144: pt ", pt) if err != nil { err = err return false } - f.paths = append(f.paths, pt) + f.paths[pt.String()] = pt default: } @@ -167,12 +177,13 @@ func (f *file) findWalkCalls() error { err = nil return false } - pt, err := pkger.Parse(s) + pt, err := f.info.Parse(s) if err != nil { err = err return false } - f.paths = append(f.paths, pt) + fmt.Println(">>>TODO parser/visitor.go:185: pt ", pt) + f.paths[pt.String()] = pt case *ast.Ident: val, ok := f.decls[x.Name] if !ok { @@ -185,12 +196,13 @@ func (f *file) findWalkCalls() error { err = nil return false } - pt, err := pkger.Parse(s) + pt, err := f.info.Parse(s) if err != nil { err = err return false } - f.paths = append(f.paths, pt) + fmt.Println(">>>TODO parser/visitor.go:204: pt ", pt) + f.paths[pt.String()] = pt default: } @@ -203,25 +215,26 @@ func (f *file) findWalkCalls() error { func (f *file) findImportCalls() error { var err error f.walk(func(node ast.Node) bool { - ce, ok := node.(*ast.ImportSpec) - if !ok { - return true - } + // ce, ok := node.(*ast.ImportSpec) + // if !ok { + // return true + // } - s, err := strconv.Unquote(ce.Path.Value) - if err != nil { - return false - } - info, err := here.Package(s) - if err != nil { - return false - } - fmt.Println(">>>TODO parser/visitor.go:216: info ", info) - res, err := Parse(info) - if err != nil { - return false - } - fmt.Println(">>>TODO parser/visitor.go:224: res ", res) + // s, err := strconv.Unquote(ce.Path.Value) + // if err != nil { + // return false + // } + // fmt.Println(">>>TODO parser/visitor.go:215: s ", s) + // info, err := here.Package(s) + // if err != nil { + // return false + // } + // fmt.Println(">>>TODO parser/visitor.go:216: info ", info) + // res, err := Parse(info) + // if err != nil { + // return false + // } + // fmt.Println(">>>TODO parser/visitor.go:224: res ", res) return true }) return err diff --git a/pkger.go b/pkger.go index 651ce56..80ae0a1 100644 --- a/pkger.go +++ b/pkger.go @@ -15,7 +15,11 @@ var current pkging.Pkger var gil = &sync.RWMutex{} var disk = func() pkging.Pkger { - n, err := stdos.New() + her, err := here.Current() + if err != nil { + log.Println(err) + } + n, err := stdos.New(her) if err != nil { log.Println(err) } @@ -31,7 +35,7 @@ func impl() pkging.Pkger { return current } -func Parse(p string) (pkging.Path, error) { +func Parse(p string) (here.Path, error) { return impl().Parse(p) } @@ -39,7 +43,7 @@ func Abs(p string) (string, error) { return impl().Abs(p) } -func AbsPath(p pkging.Path) (string, error) { +func AbsPath(p here.Path) (string, error) { return impl().AbsPath(p) } diff --git a/pkger_test.go b/pkger_test.go index df4c298..fabcf3f 100644 --- a/pkger_test.go +++ b/pkger_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "testing" - "github.com/markbates/pkger/pkging" + "github.com/markbates/pkger/here" "github.com/stretchr/testify/require" ) @@ -32,7 +32,7 @@ func Test_Abs(t *testing.T) { func Test_AbsPath(t *testing.T) { r := require.New(t) - s, err := AbsPath(pkging.Path{ + s, err := AbsPath(here.Path{ Pkg: "github.com/markbates/pkger", Name: "/rocket.ship", }) diff --git a/pkging/file.go b/pkging/file.go index 1fad69d..167f46d 100644 --- a/pkging/file.go +++ b/pkging/file.go @@ -13,7 +13,7 @@ type File interface { Info() here.Info Name() string Open(name string) (http.File, error) - Path() Path + Path() here.Path Read(p []byte) (int, error) Readdir(count int) ([]os.FileInfo, error) Seek(offset int64, whence int) (int64, error) diff --git a/pkging/mem/embed_test.go b/pkging/mem/embed_test.go index f75af46..7845feb 100644 --- a/pkging/mem/embed_test.go +++ b/pkging/mem/embed_test.go @@ -35,7 +35,14 @@ func Test_Pkger_MarshalEmbed(t *testing.T) { pinfo, err := p2.Current() r.NoError(err) - r.Equal(info, pinfo) + r.Equal(info.ImportPath, pinfo.ImportPath) + r.Equal(info.Name, pinfo.Name) + r.Equal(info.Imports, pinfo.Imports) + + m1 := info.Module + m2 := pinfo.Module + r.Equal(m1.Main, m2.Main) + r.Equal(m1.GoVersion, m2.GoVersion) var act []os.FileInfo err = p2.Walk("/", func(path string, info os.FileInfo, err error) error { diff --git a/pkging/mem/file.go b/pkging/mem/file.go index 8bb1c07..1ede642 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -23,9 +23,9 @@ var _ pkging.File = &File{} type File struct { info *pkging.FileInfo her here.Info - path pkging.Path + path here.Path data []byte - parent pkging.Path + parent here.Path writer *bytes.Buffer reader io.Reader pkging pkging.Pkger @@ -34,9 +34,9 @@ type File struct { type fJay struct { Info *pkging.FileInfo `json:"info"` Her here.Info `json:"her"` - Path pkging.Path `json:"path"` + Path here.Path `json:"path"` Data []byte `json:"data"` - Parent pkging.Path `json:"parent"` + Parent here.Path `json:"parent"` } func (f File) MarshalJSON() ([]byte, error) { @@ -134,7 +134,7 @@ func (f File) Abs() (string, error) { return f.pkging.AbsPath(f.Path()) } -func (f File) Path() pkging.Path { +func (f File) Path() here.Path { return f.path } diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index 1ced4c5..1c0dc9a 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -25,10 +25,7 @@ func WithInfo(fx *Pkger, infos ...here.Info) { func New(info here.Info) (*Pkger, error) { f := &Pkger{ - infos: &maps.Infos{}, - paths: &maps.Paths{ - Current: info, - }, + infos: &maps.Infos{}, files: &maps.Files{}, current: info, } @@ -39,14 +36,12 @@ func New(info here.Info) (*Pkger, error) { type Pkger struct { infos *maps.Infos - paths *maps.Paths files *maps.Files 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"` } @@ -54,7 +49,7 @@ type jay struct { func (p *Pkger) MarshalJSON() ([]byte, error) { files := map[string]*File{} - p.files.Range(func(key pkging.Path, file pkging.File) bool { + p.files.Range(func(key here.Path, file pkging.File) bool { f, ok := file.(*File) if !ok { return true @@ -65,7 +60,6 @@ func (p *Pkger) MarshalJSON() ([]byte, error) { return json.Marshal(jay{ Infos: p.infos, - Paths: p.paths, Files: files, Current: p.current, }) @@ -82,9 +76,6 @@ func (p *Pkger) UnmarshalJSON(b []byte) error { 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) @@ -104,7 +95,7 @@ func (f *Pkger) Abs(p string) (string, error) { return f.AbsPath(pt) } -func (f *Pkger) AbsPath(pt pkging.Path) (string, error) { +func (f *Pkger) AbsPath(pt here.Path) (string, error) { return pt.String(), nil } @@ -121,8 +112,8 @@ func (f *Pkger) Info(p string) (here.Info, error) { return info, nil } -func (f *Pkger) Parse(p string) (pkging.Path, error) { - return f.paths.Parse(p) +func (f *Pkger) Parse(p string) (here.Path, error) { + return f.current.Parse(p) } func (fx *Pkger) Remove(name string) error { @@ -145,19 +136,13 @@ func (fx *Pkger) RemoveAll(name string) error { return err } - fx.files.Range(func(key pkging.Path, file pkging.File) bool { + fx.files.Range(func(key here.Path, file pkging.File) bool { if strings.HasPrefix(key.Name, pt.Name) { fx.files.Delete(key) } return true }) - fx.paths.Range(func(key string, value pkging.Path) bool { - if strings.HasPrefix(key, pt.Name) { - fx.paths.Delete(key) - } - return true - }) return nil } @@ -235,7 +220,7 @@ func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error { return err } for root != "" { - pt := pkging.Path{ + pt := here.Path{ Pkg: path.Pkg, Name: root, } diff --git a/pkging/pkger.go b/pkging/pkger.go index 1ecb8e2..0729268 100644 --- a/pkging/pkger.go +++ b/pkging/pkger.go @@ -8,9 +8,9 @@ import ( ) type Pkger interface { - Parse(p string) (Path, error) + Parse(p string) (here.Path, error) Abs(p string) (string, error) - AbsPath(Path) (string, error) + AbsPath(here.Path) (string, error) Current() (here.Info, error) Info(p string) (here.Info, error) diff --git a/pkging/pkgtest/suite.go b/pkging/pkgtest/suite.go index 66af56f..62bcad3 100644 --- a/pkging/pkgtest/suite.go +++ b/pkging/pkgtest/suite.go @@ -11,6 +11,7 @@ import ( "strings" "testing" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging/pkgutil" "github.com/stretchr/testify/require" @@ -323,17 +324,17 @@ func (s Suite) Test_Parse(t *testing.T) { ip := cur.ImportPath table := []struct { in string - exp pkging.Path + exp here.Path }{ - {in: mould, exp: pkging.Path{Pkg: ip, Name: mould}}, - {in: filepath.Join(cur.Dir, mould), exp: pkging.Path{Pkg: ip, Name: mould}}, - {in: ":" + mould, exp: pkging.Path{Pkg: ip, Name: mould}}, - {in: ip + ":" + mould, exp: pkging.Path{Pkg: ip, Name: mould}}, - {in: ip, exp: pkging.Path{Pkg: ip, Name: "/"}}, - {in: ":", exp: pkging.Path{Pkg: ip, Name: "/"}}, - {in: husker + ":" + mould, exp: pkging.Path{Pkg: husker, Name: mould}}, - {in: husker, exp: pkging.Path{Pkg: husker, Name: "/"}}, - {in: husker + ":", exp: pkging.Path{Pkg: husker, Name: "/"}}, + {in: mould, exp: here.Path{Pkg: ip, Name: mould}}, + {in: filepath.Join(cur.Dir, mould), exp: here.Path{Pkg: ip, Name: mould}}, + {in: ":" + mould, exp: here.Path{Pkg: ip, Name: mould}}, + {in: ip + ":" + mould, exp: here.Path{Pkg: ip, Name: mould}}, + {in: ip, exp: here.Path{Pkg: ip, Name: "/"}}, + {in: ":", exp: here.Path{Pkg: ip, Name: "/"}}, + {in: husker + ":" + mould, exp: here.Path{Pkg: husker, Name: mould}}, + {in: husker, exp: here.Path{Pkg: husker, Name: "/"}}, + {in: husker + ":", exp: here.Path{Pkg: husker, Name: "/"}}, } for _, tt := range table { diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 0488385..e762fea 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -1,6 +1,7 @@ package stdos import ( + "fmt" "net/http" "os" "path" @@ -15,7 +16,7 @@ type File struct { *os.File info *pkging.FileInfo her here.Info - path pkging.Path + path here.Path pkging pkging.Pkger } @@ -25,6 +26,7 @@ func NewFile(fx pkging.Pkger, osf *os.File) (*File, error) { if err != nil { return nil, err } + fmt.Println(">>>TODO pkging/stdos/file.go:29: pt ", pt) info, err := osf.Stat() if err != nil { return nil, err @@ -82,7 +84,7 @@ func (f *File) Open(name string) (http.File, error) { return f2, nil } -func (f *File) Path() pkging.Path { +func (f *File) Path() here.Path { return f.path } diff --git a/pkging/stdos/file_test.go b/pkging/stdos/file_test.go index 625afcc..e9eb4a1 100644 --- a/pkging/stdos/file_test.go +++ b/pkging/stdos/file_test.go @@ -7,13 +7,16 @@ import ( "path/filepath" "testing" + "github.com/markbates/pkger/here" "github.com/stretchr/testify/require" ) func Test_File_Stat_No_Info(t *testing.T) { r := require.New(t) - pkg, err := New() + her, err := here.Current() + r.NoError(err) + pkg, err := New(her) r.NoError(err) f, err := pkg.Open(":/pkging/stdos/file_test.go") @@ -35,7 +38,9 @@ func Test_File_Stat_No_Info(t *testing.T) { func Test_File_HTTP_Dir(t *testing.T) { r := require.New(t) - pkg, err := New() + her, err := here.Current() + r.NoError(err) + pkg, err := New(her) r.NoError(err) fp := filepath.Join("..", "..", "examples", "app", "public") diff --git a/pkging/stdos/stdos.go b/pkging/stdos/stdos.go index f5cde28..6e9adb9 100644 --- a/pkging/stdos/stdos.go +++ b/pkging/stdos/stdos.go @@ -14,9 +14,8 @@ import ( var _ pkging.Pkger = &Pkger{} type Pkger struct { - infos *maps.Infos - paths *maps.Paths - current here.Info + Here here.Info + infos *maps.Infos } func (f *Pkger) Abs(p string) (string, error) { @@ -27,9 +26,9 @@ func (f *Pkger) Abs(p string) (string, error) { return f.AbsPath(pt) } -func (f *Pkger) AbsPath(pt pkging.Path) (string, error) { - if pt.Pkg == f.current.ImportPath { - return filepath.Join(f.current.Dir, pt.Name), nil +func (f *Pkger) AbsPath(pt here.Path) (string, error) { + if pt.Pkg == f.Here.ImportPath { + return filepath.Join(f.Here.Dir, pt.Name), nil } info, err := f.Info(pt.Pkg) if err != nil { @@ -38,19 +37,12 @@ func (f *Pkger) AbsPath(pt pkging.Path) (string, error) { return filepath.Join(info.Dir, pt.Name), nil } -func New() (*Pkger, error) { - info, err := here.Current() - if err != nil { - return nil, err - } +func New(her here.Info) (*Pkger, error) { p := &Pkger{ infos: &maps.Infos{}, - paths: &maps.Paths{ - Current: info, - }, - current: info, + Here: her, } - p.infos.Store(info.ImportPath, info) + p.infos.Store(her.ImportPath, her) return p, nil } @@ -67,7 +59,7 @@ func (fx *Pkger) Create(name string) (pkging.File, error) { } func (f *Pkger) Current() (here.Info, error) { - return f.current, nil + return f.Here, nil } func (f *Pkger) Info(p string) (here.Info, error) { @@ -104,8 +96,8 @@ func (fx *Pkger) Open(name string) (pkging.File, error) { return NewFile(fx, f) } -func (f *Pkger) Parse(p string) (pkging.Path, error) { - return f.paths.Parse(p) +func (f *Pkger) Parse(p string) (here.Path, error) { + return f.Here.Parse(p) } func (f *Pkger) Stat(name string) (os.FileInfo, error) { diff --git a/pkging/stdos/stdos_test.go b/pkging/stdos/stdos_test.go index 09e31c1..3a97593 100644 --- a/pkging/stdos/stdos_test.go +++ b/pkging/stdos/stdos_test.go @@ -4,24 +4,28 @@ import ( "io/ioutil" "testing" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging/pkgtest" ) func Test_Pkger(t *testing.T) { suite, err := pkgtest.NewSuite("stdos", func() (pkging.Pkger, error) { - mypkging, err := New() + her, err := here.Current() if err != nil { return nil, err } - dir, err := ioutil.TempDir("", "stdos") if err != nil { return nil, err } - mypkging.current.Dir = dir - mypkging.paths.Current = mypkging.current + her.Dir = dir + + mypkging, err := New(her) + if err != nil { + return nil, err + } return mypkging, nil }) diff --git a/pkging/wrap.go b/pkging/wrap.go index 20d0b5d..5ae8ca1 100644 --- a/pkging/wrap.go +++ b/pkging/wrap.go @@ -27,7 +27,7 @@ func (w withPkger) String() string { return fmt.Sprintf("%T > %T", w.base, w.parent) } -func (w withPkger) Parse(p string) (Path, error) { +func (w withPkger) Parse(p string) (here.Path, error) { pt, err := w.base.Parse(p) if err != nil { if w.parent != nil { @@ -49,7 +49,7 @@ func (w withPkger) Abs(p string) (string, error) { return pt, nil } -func (w withPkger) AbsPath(p Path) (string, error) { +func (w withPkger) AbsPath(p here.Path) (string, error) { pt, err := w.base.AbsPath(p) if err != nil { if w.parent != nil { diff --git a/stuffing/stuffing.go b/stuffing/stuffing.go index 3929c74..c374dae 100644 --- a/stuffing/stuffing.go +++ b/stuffing/stuffing.go @@ -1,7 +1,9 @@ package stuffing import ( + "fmt" "io" + "strings" "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" @@ -9,8 +11,8 @@ import ( "github.com/markbates/pkger/pkging/stdos" ) -func Stuff(w io.Writer, cur here.Info, paths []pkging.Path) error { - disk, err := stdos.New() +func Stuff(w io.Writer, cur here.Info, paths []here.Path) error { + disk, err := stdos.New(cur) if err != nil { return err } @@ -32,6 +34,10 @@ func Stuff(w io.Writer, cur here.Info, paths []pkging.Path) error { if err != nil { return err } + info := f.Info() + fmt.Println(">>>TODO stuffing/stuffing.go:41: info.Dir ", info.Dir) + fi = pkging.WithName(strings.TrimPrefix(fi.Name(), info.Dir), fi) + fmt.Println(">>>TODO stuffing/stuffing.go:37: fi.Name() ", fi.Name()) if err := pkg.Add(fi, f); err != nil { return err