Compare commits

..

40 Commits

Author SHA1 Message Date
Mark Bates 0273362499
Merge pull request #102 from requaos/requaos/gzip
Remove hepa library for revision at a later date
2020-08-12 13:53:52 -04:00
Neil Skinner c78e87acb0 Updating documentation to reflect the paths generated without the hepa library 2020-06-10 09:31:24 -04:00
Neil Skinner 143afe6a94 remove post-processing library hepa; pending revision 2020-06-09 10:09:43 -04:00
Mark Bates 992ea93a2a
Merge pull request #97 from qmuntal/master
Support const paths
2020-06-03 14:03:53 -04:00
Mark Bates d84a13e7c2
Merge pull request #101 from requaos/requaos/gzip
Apply hepa filters before gzip'ing content
2020-06-03 14:03:12 -04:00
Neil Skinner fdcec3a4ea Apply hepa filters before gzip'ing content
- markbates/pkger#26
2020-06-03 12:46:06 -04:00
Quim Muntal Diaz c7908c9bfc support const expressions 2020-05-21 17:19:19 +02:00
Mark Bates d97c60c346
Merge pull request #93 from xDuck/master
Fixes high cache miss rate, significant performance increase.
2020-05-15 17:09:09 -04:00
xDuck efed726c9e Fixes high cache miss rate, significant performance increase. Closes #92 2020-05-15 15:34:27 -04:00
Mark Bates c028e66556
Merge pull request #82 from xDuck/master
Add conditional build tag to generated files
2020-04-03 10:58:08 -04:00
Mark Bates 8554e69817
Merge pull request #83 from Bios-Marcel/patch-1
Fix a small typo
2020-04-03 10:57:47 -04:00
Marcel Schramm b4563ccbec
Fix a small typo 2020-03-27 20:01:47 +01:00
Chris 3c88c54d65
Removed optional tags with one tag, !skippkger 2020-03-24 14:14:21 -04:00
Chris 331c57067b
Moved build tags below code generation warning 2020-03-24 14:02:20 -04:00
Chris bc0ae93dc6
Added -t for specifying conditional build tags 2020-03-24 13:51:10 -04:00
Mark Bates a78c11e368
Merge pull request #80 from taiyangc/master
Fix pkger generating the wrong package name for pkger.go
2020-03-11 14:37:04 -04:00
Eric Chen 9552dcf46d Fix pkger generating the wrong package name for pkger.go
The issue is that, if pkger.go is created (empty) before the `go list
-json` call it will fail and give the generic directory name instead of the
actual package name under the directory. This is fine in most cases,
except when the package name differs from the directory name - which is
allowed. Then pkger.go will conflict and fail to compile with the rest
of *.go under the same directory.

To fix, we simply obtain the real package name before creating the dummy
pkger.go file and then pass the name into the file.
2020-03-11 03:31:03 -07:00
Mark Bates 5b8abbabca
Merge pull request #77 from ghouscht/issue-76
fix pkger error if a project contains a .go directory #76
2020-03-04 09:55:04 -05:00
Thomas Gosteli 5d81ba66cb fix pkger error if a project contains a .go directory #76 2020-03-04 14:33:57 +01:00
Mark Bates c9b56cd758
Merge pull request #70 from hnnsgstfssn/fix-typo
Fix typo
2020-02-17 16:29:53 -05:00
Hannes Gustafsson 9925b6cc05 Fix typo
4e1fe7b43f introduced a Fprintf call that
passes a variable without a formatting directive. This will cause go vet
-printf to fail with

    cmd/pkger/cmds/pack.go:150: no formatting directive in Fprintf call

Drop the variable from the call.
2020-02-16 22:05:56 +00:00
Mark Bates defb2921c0
Merge pull request #69 from hnnsgstfssn/add-generated-code-disclaimer
Add generated code disclaimer
2020-02-16 15:13:34 -05:00
Hannes Gustafsson 4e1fe7b43f Add generated code disclaimer
Generated code should contain a disclaimer as per [1].

Add a disclaimer to the top of the file generated by the pack command.

Fixes #68.

[1] https://golang.org/s/generatedcode
2020-02-16 19:54:06 +00:00
Mark Bates d9d5139985
Merge pull request #61 from markbates/fix-external-pkgs
Fixes issues with `<pkg>:/<name>` paths not resolving correctly.
2020-01-09 15:48:40 -05:00
Mark Bates 4a3675581d use pkg info from string parse 2020-01-08 17:37:55 -05:00
Mark Bates c5e820d039
Merge pull request #50 from markbates/here-here
use gobuffalo/here fixes #49
2019-12-17 10:19:58 -05:00
Mark Bates db2f21a8c7 see yourself 2019-12-17 10:16:55 -05:00
Mark Bates 7a3189e727 Merge branch 'master' into here-here 2019-12-17 10:12:20 -05:00
Mark Bates 6217c988eb
Merge pull request #55 from moqmar/master
Provide workaround for segmentation fault (fixes #54)
2019-12-17 10:08:08 -05:00
Moritz Marquardt 2a4daae578
Provide workaround for segmentation fault (fixes #54) 2019-12-16 09:09:58 +01:00
Mark Bates 6c1b23a5cb new include 2019-12-13 11:24:17 -05:00
Mark Bates 5497054b42
Merge pull request #53 from markbates/handle-main
handle main packages
2019-12-11 15:15:54 -05:00
Mark Bates 6431634511 better error 2019-12-03 17:40:04 -05:00
Mark Bates 0167919fce ignore .tmp stuff 2019-12-03 17:36:58 -05:00
Mark Bates e4d652a24e revert 2019-12-03 15:32:08 -05:00
Mark Bates ec8c803b27 verbs 2019-12-03 15:29:17 -05:00
Mark Bates b24faf1070 get -t 2019-12-03 15:23:12 -05:00
Mark Bates 94798bc8a6 remove 1.12 2019-12-03 15:13:55 -05:00
Mark Bates 10f43292e9 github stuff 2019-12-03 15:11:25 -05:00
Mark Bates 16972f8f18 use gobuffalo/here fixes #49 2019-12-03 15:07:51 -05:00
44 changed files with 219 additions and 6930 deletions

4
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: markbates
patreon: buffalo

View File

@ -16,7 +16,7 @@ jobs:
go-version: 1.13 go-version: 1.13
id: go id: go
- -
name: Checkout name: Checkout Code
uses: actions/checkout@master uses: actions/checkout@master
- -
name: Run GoReleaser name: Run GoReleaser

View File

@ -24,9 +24,10 @@ Paths:
* Packages can specified in at the beginning of a path with a `:` seperator. * Packages can specified in at the beginning of a path with a `:` seperator.
github.com/markbates/pkger:/cmd/pkger/main.go github.com/markbates/pkger:/cmd/pkger/main.go
* There are no relative paths. All paths are absolute to the modules root. * There are no relative paths. All paths are absolute to the modules root.
* Fully-qualified paths are embedded into the metadata of your static assets. If this behavior is undesirable, a preference is to build in a containerized environ, like docker, where the path strings are not ex-filtrating data about your development environment.
``` ```
"github.com/gobuffalo/buffalo:/go.mod" => $GOPATH/pkg/mod/github.com/gobuffalo/buffalo@v0.14.7/go.mod "github.com/gobuffalo/buffalo:/go.mod" => /go/pkg/mod/github.com/gobuffalo/buffalo@v0.14.7/go.mod
``` ```
## CLI ## CLI
@ -53,7 +54,7 @@ package <.>
// Pkger stuff here // Pkger stuff here
``` ```
The `-o` flag can be used specify the directory of the `pkged.go` file. The `-o` flag can be used to specify the directory of the `pkged.go` file.
```bash ```bash
$ pkger -o cmd/reader $ pkger -o cmd/reader
@ -284,26 +285,26 @@ $ pkger parse
".": [ ".": [
{ {
"file": { "file": {
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz", "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz",
"Path": { "Path": {
"Pkg": "app", "Pkg": "app",
"Name": "/foo/bar/baz" "Name": "/foo/bar/baz"
}, },
"Here": { "Here": {
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
"ImportPath": "app", "ImportPath": "app",
"Module": { "Module": {
"Path": "app", "Path": "app",
"Main": true, "Main": true,
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod", "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
"GoVersion": "1.13" "GoVersion": "1.13"
}, },
"Name": "main" "Name": "main"
} }
}, },
"pos": { "pos": {
"Filename": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go", "Filename": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
"Offset": 629, "Offset": 629,
"Line": 47, "Line": 47,
"Column": 27 "Column": 27
@ -313,26 +314,26 @@ $ pkger parse
}, },
{ {
"file": { "file": {
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz/biz.txt", "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz/biz.txt",
"Path": { "Path": {
"Pkg": "app", "Pkg": "app",
"Name": "/foo/bar/baz/biz.txt" "Name": "/foo/bar/baz/biz.txt"
}, },
"Here": { "Here": {
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
"ImportPath": "app", "ImportPath": "app",
"Module": { "Module": {
"Path": "app", "Path": "app",
"Main": true, "Main": true,
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod", "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
"GoVersion": "1.13" "GoVersion": "1.13"
}, },
"Name": "main" "Name": "main"
} }
}, },
"pos": { "pos": {
"Filename": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go", "Filename": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
"Offset": 706, "Offset": 706,
"Line": 51, "Line": 51,
"Column": 25 "Column": 25
@ -388,38 +389,38 @@ $ pkger list -json
"ImportPath": "app", "ImportPath": "app",
"Files": [ "Files": [
{ {
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets", "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
"Path": { "Path": {
"Pkg": "app", "Pkg": "app",
"Name": "/assets" "Name": "/assets"
}, },
"Here": { "Here": {
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
"ImportPath": "", "ImportPath": "",
"Module": { "Module": {
"Path": "app", "Path": "app",
"Main": true, "Main": true,
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod", "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
"GoVersion": "1.13" "GoVersion": "1.13"
}, },
"Name": "assets" "Name": "assets"
} }
}, },
{ {
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets/css", "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets/css",
"Path": { "Path": {
"Pkg": "app", "Pkg": "app",
"Name": "/assets/css" "Name": "/assets/css"
}, },
"Here": { "Here": {
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
"ImportPath": "", "ImportPath": "",
"Module": { "Module": {
"Path": "app", "Path": "app",
"Main": true, "Main": true,
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref", "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod", "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
"GoVersion": "1.13" "GoVersion": "1.13"
}, },
"Name": "assets" "Name": "assets"

View File

@ -9,8 +9,6 @@ import (
"path/filepath" "path/filepath"
"github.com/markbates/pkger" "github.com/markbates/pkger"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
) )
@ -71,15 +69,7 @@ func (e *listCmd) Exec(args []string) error {
return err return err
} }
hep := hepa.New() _, err = os.Stdout.Write(bb.Bytes())
hep = hepa.With(hep, filters.Home())
hep = hepa.With(hep, filters.Golang())
b, err := hep.Filter(bb.Bytes())
if err != nil {
return err
}
_, err = os.Stdout.Write(b)
return err return err
} }

View File

@ -124,6 +124,11 @@ func (e *packCmd) Flags() *flag.FlagSet {
} }
func Package(info here.Info, out string, decls parser.Decls) error { func Package(info here.Info, out string, decls parser.Decls) error {
c, err := here.Dir(filepath.Dir(out))
if err != nil {
return err
}
os.RemoveAll(out) os.RemoveAll(out)
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -137,17 +142,9 @@ func Package(info here.Info, out string, decls parser.Decls) error {
} }
defer f.Close() defer f.Close()
c, err := here.Dir(filepath.Dir(out)) fmt.Fprintf(f, "// Code generated by pkger; DO NOT EDIT.\n\n")
if err != nil { fmt.Fprintf(f, "// +build !skippkger\n\n")
return err fmt.Fprintf(f, "package %s\n\n", c.Name)
}
name := c.Name
if info.Module.Main {
name = "main"
}
fmt.Fprintf(f, "package %s\n\n", name)
fmt.Fprintf(f, "import (\n\t\"github.com/markbates/pkger\"\n\t") fmt.Fprintf(f, "import (\n\t\"github.com/markbates/pkger\"\n\t")
fmt.Fprintf(f, "\"github.com/markbates/pkger/pkging/mem\"\n)\n\n") fmt.Fprintf(f, "\"github.com/markbates/pkger/pkging/mem\"\n)\n\n")
fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`")

View File

@ -1,6 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/here v0.5.2-0.20191203194143-8e97fc9ad6d3 h1:9NGxHWbGFujaZ5vT+uXurb9m3QoWa175cYM2Qs97nQo=
github.com/gobuffalo/here v0.5.2-0.20191203194143-8e97fc9ad6d3/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 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/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -19,3 +23,4 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

5
go.mod
View File

@ -3,9 +3,6 @@ module github.com/markbates/pkger
go 1.13 go 1.13
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/gobuffalo/here v0.6.0
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.5 // indirect
) )

7
go.sum
View File

@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 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/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -15,7 +17,6 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -1,29 +0,0 @@
package here
import (
"path/filepath"
"sync"
)
var curOnce sync.Once
var curErr error
var current Info
func Current() (Info, error) {
(&curOnce).Do(func() {
b, err := run("go", "env", "GOMOD")
if err != nil {
curErr = err
return
}
root := filepath.Dir(string(b))
i, err := Dir(root)
if err != nil {
curErr = err
return
}
current = i
})
return current, curErr
}

View File

@ -1,77 +0,0 @@
package here
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
// Dir attempts to gather info for the requested directory.
func Dir(p string) (Info, error) {
i, err := Cache(p, func(p string) (Info, error) {
var i Info
fi, err := os.Stat(p)
if err != nil {
return i, err
}
if !fi.IsDir() {
p = filepath.Dir(p)
}
pwd, err := os.Getwd()
if err != nil {
return i, err
}
defer os.Chdir(pwd)
os.Chdir(p)
b, err := run("go", "list", "-json")
// go: cannot find main module; see 'go help modules'
// build .: cannot find module for path .
// no Go files in
if err != nil {
if nonGoDirRx.MatchString(err.Error()) {
return fromNonGoDir(p)
}
return i, fmt.Errorf("%w %s", err, p)
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
return i, nil
})
if err != nil {
return i, err
}
return Cache(i.ImportPath, func(p string) (Info, error) {
return i, nil
})
}
func fromNonGoDir(dir string) (Info, error) {
i := Info{
Dir: dir,
Name: filepath.Base(dir),
}
b, err := run("go", "list", "-json", "-m")
if err != nil {
return i, err
}
if err := json.Unmarshal(b, &i.Module); err != nil {
return i, err
}
return i, err
}

View File

@ -1,67 +0,0 @@
package here_test
import (
"os"
"path/filepath"
"testing"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
func Test_Dir(t *testing.T) {
r := require.New(t)
ref, err := pkgtest.NewRef()
r.NoError(err)
root := ref.Dir
r.NoError(err)
defer os.RemoveAll(root)
public := filepath.Join(root, "public")
r.NoError(os.MkdirAll(public, 0755))
gf := filepath.Join(root, "cmd", "main.go")
r.NoError(os.MkdirAll(filepath.Dir(gf), 0755))
f, err := os.Create(gf)
r.NoError(err)
_, err = f.Write([]byte("package main"))
r.NoError(err)
r.NoError(f.Close())
table := []struct {
in string
err bool
}{
{in: root, err: false},
{in: public, err: false},
{in: gf, err: false},
{in: filepath.Join(root, "."), err: false},
{in: "/unknown", err: true},
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
here.ClearCache()
r := require.New(st)
info, err := here.Dir(tt.in)
if tt.err {
r.Error(err)
return
}
r.NoError(err)
r.NotZero(info)
r.NotZero(info.Dir)
r.NotZero(info.Name)
r.NotZero(info.Module)
})
}
}

View File

@ -1,49 +1,14 @@
package here package here
import ( import (
"bytes" "github.com/gobuffalo/here"
"fmt"
"os/exec"
"regexp"
"strings"
"sync"
) )
var cache = &infoMap{ type Info = here.Info
data: &sync.Map{}, type Module = here.Module
} type Path = here.Path
func run(n string, args ...string) ([]byte, error) { var Here = here.New()
c := exec.Command(n, args...) var Dir = Here.Dir
var Package = Here.Package
bb := &bytes.Buffer{} var Current = Here.Current
c.Stdout = bb
c.Stderr = bb
err := c.Run()
if err != nil {
return nil, fmt.Errorf("%w: %q: %s", err, strings.Join(c.Args, " "), bb)
}
return bb.Bytes(), nil
}
func Cache(p string, fn func(string) (Info, error)) (Info, error) {
i, ok := cache.Load(p)
if ok {
return i, nil
}
i, err := fn(p)
if err != nil {
return i, err
}
cache.Store(p, i)
return i, nil
}
func ClearCache() {
cache = &infoMap{
data: &sync.Map{},
}
}
var nonGoDirRx = regexp.MustCompile(`cannot find main|go help modules|go: |build .:|no Go files|can't load package`)

View File

@ -1,33 +0,0 @@
package here
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_nonGoDirRx(t *testing.T) {
r := require.New(t)
r.False(nonGoDirRx.MatchString(""))
r.False(nonGoDirRx.MatchString("hello"))
table := []string{
"go: cannot find main module; see 'go help modules'",
"go help modules",
"go: ",
"build .:",
"no Go files",
"can't load package: package",
}
for _, tt := range table {
t.Run(tt, func(st *testing.T) {
r := require.New(st)
b := nonGoDirRx.MatchString(tt)
r.True(b)
})
}
}

View File

@ -1,39 +0,0 @@
package here
import (
"encoding/json"
)
// Info represents details about the directory/package
type Info struct {
Dir string
ImportPath string
Name string
Module Module
}
func (i Info) MarshalJSON() ([]byte, error) {
mm := map[string]interface{}{
"ImportPath": i.ImportPath,
"Name": i.Name,
"Module": i.Module,
"Dir": i.Dir,
}
return json.Marshal(mm)
}
// IsZero checks if the type has been filled
// with rich chocolately data goodness
func (i Info) IsZero() bool {
return i.String() == Info{}.String()
}
func (i Info) String() string {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return err.Error()
}
s := string(b)
return s
}

View File

@ -1,87 +0,0 @@
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package here
import (
"sort"
"sync"
)
// infoMap wraps sync.Map and uses the following types:
// key: string
// value: Info
type infoMap struct {
data *sync.Map
}
// Delete the key from the map
func (m *infoMap) Delete(key string) {
m.data.Delete(key)
}
// Load the key from the map.
// Returns Info or bool.
// A false return indicates either the key was not found
// or the value is not of type Info
func (m *infoMap) Load(key string) (Info, bool) {
i, ok := m.data.Load(key)
if !ok {
return Info{}, false
}
s, ok := i.(Info)
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *infoMap) LoadOrStore(key string, value Info) (Info, bool) {
i, _ := m.data.LoadOrStore(key, value)
s, ok := i.(Info)
return s, ok
}
// LoadOr will return an existing key or
// run the function and store the results
func (m *infoMap) LoadOr(key string, fn func(*infoMap) (Info, bool)) (Info, bool) {
i, ok := m.Load(key)
if ok {
return i, ok
}
i, ok = fn(m)
if ok {
m.Store(key, i)
return i, ok
}
return i, false
}
// Range over the Info values in the map
func (m *infoMap) Range(f func(key string, value Info) bool) {
m.data.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return false
}
value, ok := v.(Info)
if !ok {
return false
}
return f(key, value)
})
}
// Store a Info in the map
func (m *infoMap) Store(key string, value Info) {
m.data.Store(key, value)
}
// Keys returns a list of keys in the map
func (m *infoMap) Keys() []string {
var keys []string
m.Range(func(key string, value Info) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)
return keys
}

View File

@ -1,25 +0,0 @@
package here
import (
"encoding/json"
)
type Module struct {
Path string
Main bool
Dir string
GoMod string
GoVersion string
}
func (i Module) String() string {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return err.Error()
}
return string(b)
}
func (i Module) IsZero() bool {
return i.String() == Module{}.String()
}

View File

@ -1,63 +0,0 @@
package here
import (
"fmt"
"path/filepath"
"regexp"
"strings"
)
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 == ":" || 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.Module.Path
}
if len(pt.Name) == 0 {
pt.Name = "/"
}
if pt.Pkg == pt.Name {
pt.Pkg = i.Module.Path
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("([^:]+)(:(/.+))?")

View File

@ -1,63 +0,0 @@
package here_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
func Test_Info_Parse(t *testing.T) {
const name = "/public/index.html"
r := require.New(t)
app, err := pkgtest.NewRef()
r.NoError(err)
defer os.RemoveAll(app.Dir)
ip := app.Info.ImportPath
ip2 := "another/app"
table := []struct {
in string
exp here.Path
err bool
}{
{in: name, exp: here.Path{Pkg: ip, Name: name}},
{in: "", exp: here.Path{Pkg: ip, Name: "/"}},
{in: "/", exp: here.Path{Pkg: ip, Name: "/"}},
{in: filepath.Join(app.Info.Dir, name), exp: here.Path{Pkg: ip, Name: name}},
{in: ":" + name, exp: here.Path{Pkg: ip, Name: name}},
{in: ip + ":" + name, exp: here.Path{Pkg: ip, Name: name}},
{in: ip, exp: here.Path{Pkg: ip, Name: "/"}},
{in: ":", exp: here.Path{Pkg: ip, Name: "/"}},
{in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}},
{in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}},
{in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}},
{in: filepath.Join(app.Info.Dir, "public"), exp: here.Path{Pkg: ip, Name: "/public"}},
}
for _, tt := range table {
for _, in := range []string{tt.in, strings.ReplaceAll(tt.in, "/", "\\")} {
t.Run(in, func(st *testing.T) {
r := require.New(st)
pt, err := app.Info.Parse(in)
if tt.err {
r.Error(err)
return
}
r.NoError(err)
r.Equal(tt.exp, pt)
})
}
}
}

View File

@ -1,20 +0,0 @@
package here
import (
"fmt"
)
type Path struct {
Pkg string
Name string
}
func (p Path) String() string {
if p.Name == "" {
p.Name = "/"
}
if p.Pkg == "" {
return p.Name
}
return fmt.Sprintf("%s:%s", p.Pkg, p.Name)
}

View File

@ -1,44 +0,0 @@
package here
import (
"encoding/json"
"fmt"
)
// Package attempts to gather info for the requested package.
//
// From the `go help list` docs:
// The -find flag causes list to identify the named packages but not
// resolve their dependencies: the Imports and Deps lists will be empty.
//
// A workaround for this issue is to use the `Dir` field in the
// returned `Info` value and pass it to the `Dir(string) (Info, error)`
// function to return the complete data.
func Package(p string) (Info, error) {
i, err := Cache(p, func(p string) (Info, error) {
var i Info
if len(p) == 0 || p == "." {
return i, fmt.Errorf("missing package name")
}
b, err := run("go", "list", "-json", "-find", p)
if err != nil {
return i, err
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
return i, nil
})
if err != nil {
return i, err
}
Cache(i.Dir, func(p string) (Info, error) {
return i, nil
})
return i, nil
}

View File

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2019 Mark Bates
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,35 +0,0 @@
package hepa
import "bytes"
type Filter interface {
Filter([]byte) ([]byte, error)
}
type FilterFunc func([]byte) ([]byte, error)
func (f FilterFunc) Filter(b []byte) ([]byte, error) {
return f(b)
}
func Rinse(p Purifier, s, r []byte) Purifier {
return WithFunc(p, func(b []byte) ([]byte, error) {
b = bytes.ReplaceAll(b, s, r)
return b, nil
})
}
func Clean(p Purifier, s []byte) Purifier {
return WithFunc(p, func(b []byte) ([]byte, error) {
if bytes.Contains(b, s) {
return []byte{}, nil
}
return b, nil
})
}
func Noop() FilterFunc {
return func(b []byte) ([]byte, error) {
return b, nil
}
}

View File

@ -1,42 +0,0 @@
package filters
import (
"os"
"strconv"
"strings"
)
var env = func() map[string]string {
m := map[string]string{}
for _, line := range os.Environ() {
kv := strings.Split(line, "=")
k, v := kv[0], kv[1]
kt, vt := strings.TrimSpace(k), strings.TrimSpace(v)
if len(kt) == 0 || len(vt) == 0 {
continue
}
switch k {
case "GO111MODULE":
continue
}
switch v {
case "true", "TRUE", "false", "FALSE", "null", "nil", "NULL":
continue
}
if _, err := strconv.Atoi(k); err == nil {
continue
}
if _, err := strconv.Atoi(v); err == nil {
continue
}
m[k] = v
}
return m
}()

View File

@ -1,12 +0,0 @@
package filters
type FilterFunc func([]byte) ([]byte, error)
func (f FilterFunc) Filter(b []byte) ([]byte, error) {
return f(b)
}
type dir struct {
Dir string
Err error
}

View File

@ -1,51 +0,0 @@
package filters
import (
"bytes"
"os"
"path/filepath"
)
func Golang() FilterFunc {
return func(b []byte) ([]byte, error) {
gp, err := gopath(home)
if err != nil {
return nil, err
}
b = bytes.ReplaceAll(b, []byte(gp.Dir), []byte("$GOPATH"))
gru, err := goroot(gp)
if err != nil {
return nil, err
}
b = bytes.ReplaceAll(b, []byte(gru.Dir), []byte("$GOROOT"))
return b, nil
}
}
func goroot(gp dir) (dir, error) {
gru, ok := os.LookupEnv("GOROOT")
if !ok {
if gp.Err != nil {
return gp, gp.Err
}
gru = filepath.Join(string(gp.Dir), "go")
}
return dir{
Dir: gru,
}, nil
}
func gopath(home dir) (dir, error) {
gp, ok := os.LookupEnv("GOPATH")
if !ok {
if home.Err != nil {
return home, home.Err
}
gp = filepath.Join(string(home.Dir), "go")
}
return dir{
Dir: gp,
}, nil
}

View File

@ -1,31 +0,0 @@
package filters
import (
"bytes"
"os"
)
var home = func() dir {
var d dir
home, ok := os.LookupEnv("HOME")
if !ok {
pwd, err := os.Getwd()
if err != nil {
d.Err = err
return d
}
home = pwd
}
d.Dir = home
return d
}()
func Home() FilterFunc {
return func(b []byte) ([]byte, error) {
if home.Err != nil {
return b, home.Err
}
return bytes.ReplaceAll(b, []byte(home.Dir), []byte("$HOME")), nil
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,29 +0,0 @@
package filters
import (
"bytes"
"strings"
)
func Secrets() FilterFunc {
return func(b []byte) ([]byte, error) {
for k, v := range env {
for _, s := range secretSuffixes {
if !strings.HasSuffix(k, s) {
continue
}
b = bytes.ReplaceAll(b, []byte(v), []byte(mask()))
break
}
}
return b, nil
}
}
var secretSuffixes = []string{
"_KEY",
"_SECRET",
"_TOKEN",
"_PASSWORD",
"_PASS",
}

View File

@ -1,15 +0,0 @@
package hepa
func WithFunc(p Purifier, fn FilterFunc) Purifier {
c := New()
c.parent = &p
c.filter = fn
return c
}
func With(p Purifier, f Filter) Purifier {
c := New()
c.parent = &p
c.filter = f
return c
}

View File

@ -1,67 +0,0 @@
package hepa
import (
"bufio"
"bytes"
"io"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
)
type Purifier struct {
parent *Purifier
filter Filter
}
func (p Purifier) Filter(b []byte) ([]byte, error) {
if p.filter == nil {
p.filter = filters.Home()
}
b, err := p.filter.Filter(b)
if err != nil {
return b, err
}
if p.parent != nil {
return p.parent.Filter(b)
}
return b, nil
}
func (p Purifier) Clean(r io.Reader) ([]byte, error) {
bb := &bytes.Buffer{}
if p.filter == nil {
if p.parent != nil {
return p.parent.Clean(r)
}
_, err := io.Copy(bb, r)
return bb.Bytes(), err
}
home := filters.Home()
reader := bufio.NewReader(r)
for {
input, _, err := reader.ReadLine()
if err != nil && err == io.EOF {
break
}
input, err = p.Filter(input)
if err != nil {
return nil, err
}
input, err = home(input)
if err != nil {
return nil, err
}
bb.Write(input)
// if len(input) > 0 {
bb.Write([]byte("\n"))
// }
}
return bb.Bytes(), nil
}
func New() Purifier {
return Purifier{}
}

View File

@ -1,4 +0,0 @@
package hepa
// Version of hepa
const Version = "v0.0.1"

View File

@ -40,7 +40,7 @@ func (decls Decls) Files() ([]*File, error) {
files, err := fl.Files(v) files, err := fl.Files(v)
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: %s", err, d) return nil, fmt.Errorf("%s: %s", err, d)
} }
for _, f := range files { for _, f := range files {

View File

@ -5,6 +5,9 @@ import (
"fmt" "fmt"
"go/token" "go/token"
"os" "os"
"path/filepath"
"github.com/markbates/pkger/here"
) )
var _ Decl = IncludeDecl{} var _ Decl = IncludeDecl{}
@ -15,6 +18,34 @@ type IncludeDecl struct {
value string value string
} }
func NewInclude(her here.Info, inc string) (IncludeDecl, error) {
var id IncludeDecl
pt, err := her.Parse(inc)
if err != nil {
return id, err
}
if pt.Pkg != her.ImportPath {
her, err = here.Package(pt.Pkg)
if err != nil {
return id, err
}
}
abs := filepath.Join(her.Module.Dir, pt.Name)
f := &File{
Abs: abs,
Path: pt,
Here: her,
}
return IncludeDecl{
value: inc,
file: f,
}, nil
}
func (d IncludeDecl) String() string { func (d IncludeDecl) String() string {
return fmt.Sprintf("pkger.Include(%q)", d.value) return fmt.Sprintf("pkger.Include(%q)", d.value)
} }

View File

@ -100,19 +100,22 @@ func (p *Parser) ParseDir(abs string, mode parser.Mode) ([]*ParsedSource, error)
her, err := here.Dir(abs) her, err := here.Dir(abs)
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: here.Dir failed %s", err, abs) return nil, fmt.Errorf("%s: here.Dir failed %s", err, abs)
} }
pt, err := her.Parse(strings.TrimPrefix(abs, filepath.Dir(abs))) pt, err := her.Parse(strings.TrimPrefix(abs, filepath.Dir(abs)))
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: here.Parse failed %s", err, abs) return nil, fmt.Errorf("%s: here.Parse failed %s", err, abs)
}
filter := func(f os.FileInfo) bool {
return !f.IsDir()
} }
fset := token.NewFileSet() fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, abs, filter, 0)
pkgs, err := parser.ParseDir(fset, abs, nil, 0)
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: ParseDir failed %s", err, abs) return nil, fmt.Errorf("%s: ParseDir failed %s", err, abs)
} }
var srcs []*ParsedSource var srcs []*ParsedSource
@ -205,13 +208,13 @@ func (p *Parser) parse() error {
srcs, err := p.ParseDir(path, 0) srcs, err := p.ParseDir(path, 0)
if err != nil { if err != nil {
return fmt.Errorf("%w: %s", err, path) return fmt.Errorf("%s: %s", err, path)
} }
for _, src := range srcs { for _, src := range srcs {
mm, err := src.DeclsMap() mm, err := src.DeclsMap()
if err != nil { if err != nil {
return fmt.Errorf("%w: %s", err, src.Abs) return fmt.Errorf("%s: %s", err, src.Abs)
} }
for k, v := range mm { for k, v := range mm {
p.decls[k] = append(p.decls[k], v...) p.decls[k] = append(p.decls[k], v...)
@ -226,30 +229,12 @@ func (p *Parser) parse() error {
func (p *Parser) parseIncludes() error { func (p *Parser) parseIncludes() error {
for _, i := range p.includes { for _, i := range p.includes {
pt, err := p.Info.Parse(i) d, err := NewInclude(p.Info, i)
if err != nil { if err != nil {
return err return err
} }
her := p.Info p.decls["Include"] = append(p.decls["Include"], d)
if pt.Pkg != her.ImportPath {
her, err = here.Package(pt.Pkg)
if err != nil {
return err
}
}
abs := filepath.Join(her.Module.Dir, pt.Name)
f := &File{
Abs: abs,
Path: pt,
Here: her,
}
p.decls["Include"] = append(p.decls["Include"], IncludeDecl{
value: i,
file: f,
})
} }
return nil return nil
} }

View File

@ -50,7 +50,6 @@ func Test_Parser_Ref_Include(t *testing.T) {
}() }()
r := require.New(t) r := require.New(t)
here.ClearCache()
ref, err := pkgtest.NewRef() ref, err := pkgtest.NewRef()
r.NoError(err) r.NoError(err)
defer os.RemoveAll(ref.Dir) defer os.RemoveAll(ref.Dir)
@ -72,10 +71,30 @@ func Test_Parser_Ref_Include(t *testing.T) {
r.Equal(26, l) r.Equal(26, l)
} }
func Test_Parser_dotGo_Directory(t *testing.T) {
r := require.New(t)
ref, err := pkgtest.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
err = os.Mkdir(filepath.Join(ref.Dir, ".go"), 0755)
r.NoError(err)
disk, err := stdos.New(ref.Info)
r.NoError(err)
_, err = pkgtest.LoadFiles("/", ref, disk)
r.NoError(err)
res, err := Parse(ref.Info)
r.NoError(err)
r.Equal(11, len(res))
}
func Test_Parser_Example_HTTP(t *testing.T) { func Test_Parser_Example_HTTP(t *testing.T) {
r := require.New(t) r := require.New(t)
here.ClearCache()
cur, err := here.Package("github.com/markbates/pkger") cur, err := here.Package("github.com/markbates/pkger")
r.NoError(err) r.NoError(err)

View File

@ -33,16 +33,39 @@ func (p *ParsedSource) Parse() error {
return p.err return p.err
} }
func (p *ParsedSource) value(node ast.Node) (string, error) { func (p *ParsedSource) valueIdent(node *ast.Ident) (s string) {
var s string s = node.Name
if node.Obj.Kind != ast.Con {
return
}
// As per ast package a Con object is always a *ValueSpec,
// but double-checking to avoid panics
if x, ok := node.Obj.Decl.(*ast.ValueSpec); ok {
// The const var can be defined inline with other vars,
// as in `const a, b = "a", "b"`.
for i, v := range x.Names {
if v.Name == node.Name {
s = p.valueNode(x.Values[i])
break
}
}
}
return
}
func (p *ParsedSource) valueNode(node ast.Node) string {
var s string
switch x := node.(type) { switch x := node.(type) {
case *ast.BasicLit: case *ast.BasicLit:
s = x.Value s = x.Value
case *ast.Ident: case *ast.Ident:
s = x.Name s = p.valueIdent(x)
} }
return s
}
func (p *ParsedSource) value(node ast.Node) (string, error) {
s := p.valueNode(node)
return strconv.Unquote(s) return strconv.Unquote(s)
} }
@ -140,26 +163,26 @@ func (p *ParsedSource) parse() error {
n := ce.Args[0] n := ce.Args[0]
val, err := p.value(n) val, err := p.value(n)
if err != nil { if err != nil {
p.err = fmt.Errorf("%w: %s", err, n) p.err = fmt.Errorf("%s: %s", err, n)
return false return false
} }
info, err := here.Dir(filepath.Dir(p.Abs)) info, err := here.Dir(filepath.Dir(p.Abs))
if err != nil { if err != nil {
p.err = fmt.Errorf("%w: %s", err, p.Abs) p.err = fmt.Errorf("%s: %s", err, p.Abs)
return false return false
} }
pt, err := info.Parse(val) pt, err := info.Parse(val)
if err != nil { if err != nil {
p.err = fmt.Errorf("%w: %s", err, p.Abs) p.err = fmt.Errorf("%s: %s", err, p.Abs)
return false return false
} }
if pt.Pkg != info.Module.Path { if pt.Pkg != info.Module.Path {
info, err = here.Package(pt.Pkg) info, err = here.Package(pt.Pkg)
if err != nil { if err != nil {
p.err = fmt.Errorf("%w: %s", err, p.Abs) p.err = fmt.Errorf("%s: %s", err, p.Abs)
return false return false
} }
} }

View File

@ -7,8 +7,6 @@ import (
"io" "io"
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
) )
func Decode(src []byte) ([]byte, error) { func Decode(src []byte) ([]byte, error) {
@ -46,16 +44,7 @@ func Encode(b []byte) ([]byte, error) {
return nil, err return nil, err
} }
hep := hepa.New() s := hex.EncodeToString(bb.Bytes())
hep = hepa.With(hep, filters.Home())
hep = hepa.With(hep, filters.Golang())
b, err := hep.Filter(bb.Bytes())
if err != nil {
return nil, err
}
s := hex.EncodeToString(b)
return []byte(s), nil return []byte(s), nil
} }

View File

@ -19,7 +19,6 @@ func (fx *Pkger) Add(files ...*os.File) error {
if err != nil { if err != nil {
return err return err
} }
pt, err := fx.Parse(f.Name()) pt, err := fx.Parse(f.Name())
if err != nil { if err != nil {
return err return err

View File

@ -1,6 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/here v0.5.2-0.20191203194143-8e97fc9ad6d3 h1:9NGxHWbGFujaZ5vT+uXurb9m3QoWa175cYM2Qs97nQo=
github.com/gobuffalo/here v0.5.2-0.20191203194143-8e97fc9ad6d3/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 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/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -18,3 +22,4 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -18,12 +18,16 @@ func main() {
} }
} }
const (
unused, pathAsset = "", "/assets"
)
func run() error { func run() error {
if err := actions.WalkTemplates(os.Stdout); err != nil { if err := actions.WalkTemplates(os.Stdout); err != nil {
return err return err
} }
err := pkger.Walk("/assets", func(path string, info os.FileInfo, err error) error { err := pkger.Walk(pathAsset, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }

View File

@ -7,7 +7,7 @@ import (
) )
func Serve() { func Serve() {
pkger.Stat("github.com/stretchr/testify:/README.md") pkger.Stat("github.com/markbates/pkger:/README.md")
dir := http.FileServer(pkger.Dir("/public")) dir := http.FileServer(pkger.Dir("/public"))
http.ListenAndServe(":3000", dir) http.ListenAndServe(":3000", dir)
} }

View File

@ -1,8 +1,10 @@
package pkgutil package pkgutil
import ( import (
"fmt"
"io" "io"
"os" "os"
"strings"
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
@ -23,9 +25,12 @@ func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
for _, pf := range files { for _, pf := range files {
err = func() error { err = func() error {
if strings.HasSuffix(pf.Abs, ".tmp") {
return nil
}
df, err := os.Open(pf.Abs) df, err := os.Open(pf.Abs)
if err != nil { if err != nil {
return err return fmt.Errorf("could open stuff %s: %s", pf.Abs, err)
} }
defer df.Close() defer df.Close()

View File

@ -19,6 +19,12 @@ func Test_Stuff(t *testing.T) {
r.NoError(err) r.NoError(err)
defer os.RemoveAll(ref.Dir) defer os.RemoveAll(ref.Dir)
pwd, err := os.Getwd()
r.NoError(err)
defer os.Chdir(pwd)
os.Chdir(ref.Dir)
disk, err := stdos.New(ref.Info) disk, err := stdos.New(ref.Info)
r.NoError(err) r.NoError(err)

View File

@ -35,16 +35,17 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
return nil, err return nil, err
} }
name = filepath.Join(fx.Here.Dir, pt.Name) her, err := fx.Info(pt.Pkg)
if err != nil {
return nil, err
}
name = filepath.Join(her.Dir, pt.Name)
f, err := os.Create(name) f, err := os.Create(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
her, err := fx.Info(pt.Pkg)
if err != nil {
return nil, err
}
nf := &File{ nf := &File{
File: f, File: f,
her: her, her: her,
@ -82,7 +83,15 @@ func (f *Pkger) Info(p string) (here.Info, error) {
// 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. // 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 (f *Pkger) MkdirAll(p string, perm os.FileMode) error { func (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
return os.MkdirAll(filepath.Join(f.Here.Dir, p), perm) pt, err := f.Parse(p)
if err != nil {
return err
}
info, err := f.Info(pt.Pkg)
if err != nil {
return err
}
return os.MkdirAll(filepath.Join(info.Dir, 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. // 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.
@ -92,13 +101,13 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
return nil, err return nil, err
} }
name = filepath.Join(fx.Here.Dir, pt.Name) her, err := fx.Info(pt.Pkg)
f, err := os.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
her, err := fx.Info(pt.Pkg) name = filepath.Join(her.Dir, pt.Name)
f, err := os.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -131,7 +140,12 @@ func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
return nil, err return nil, err
} }
info, err := os.Stat(filepath.Join(fx.Here.Dir, pt.Name)) her, err := fx.Info(pt.Pkg)
if err != nil {
return nil, err
}
info, err := os.Stat(filepath.Join(her.Dir, pt.Name))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -148,19 +162,27 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
return err return err
} }
fp := filepath.Join(f.Here.Dir, pt.Name)
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
pt, err := f.Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
if err != nil {
return err
}
info, err := f.Info(pt.Pkg) info, err := f.Info(pt.Pkg)
if err != nil { if err != nil {
return err return err
} }
fp := filepath.Join(info.Dir, pt.Name)
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
pt, err := f.Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
if err != nil {
return err
}
info, err := f.Info(pt.Pkg)
if err != nil {
return err
}
path = strings.TrimPrefix(path, info.Dir) path = strings.TrimPrefix(path, info.Dir)
path = strings.ReplaceAll(path, "\\", "/") path = strings.ReplaceAll(path, "\\", "/")
pt.Name = path pt.Name = path
@ -176,7 +198,13 @@ func (fx *Pkger) Remove(name string) error {
if err != nil { if err != nil {
return err return err
} }
return os.Remove(filepath.Join(fx.Here.Dir, pt.Name))
info, err := fx.Info(pt.Pkg)
if err != nil {
return err
}
return os.Remove(filepath.Join(info.Dir, pt.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). // 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).
@ -185,5 +213,11 @@ func (fx *Pkger) RemoveAll(name string) error {
if err != nil { if err != nil {
return err return err
} }
return os.RemoveAll(filepath.Join(fx.Here.Dir, pt.Name))
info, err := fx.Info(pt.Pkg)
if err != nil {
return err
}
return os.RemoveAll(filepath.Join(info.Dir, pt.Name))
} }