forked from mirror/pkger
Compare commits
1 Commits
master
...
github-act
Author | SHA1 | Date |
---|---|---|
Mark Bates | d0ad7b40d1 |
|
@ -1,4 +0,0 @@
|
|||
# These are supported funding model platforms
|
||||
|
||||
github: markbates
|
||||
patreon: buffalo
|
|
@ -16,7 +16,7 @@ jobs:
|
|||
go-version: 1.13
|
||||
id: go
|
||||
-
|
||||
name: Checkout Code
|
||||
name: Checkout
|
||||
uses: actions/checkout@master
|
||||
-
|
||||
name: Run GoReleaser
|
||||
|
|
|
@ -5,18 +5,6 @@ builds:
|
|||
- CGO_ENABLED=0
|
||||
ldflags:
|
||||
- -s -w -X "github.com/markbates/pkger.Version={{.Tag}}"
|
||||
goos:
|
||||
- darwin
|
||||
- linux
|
||||
- windows
|
||||
goarch:
|
||||
- amd64
|
||||
- 386
|
||||
- arm
|
||||
- arm64
|
||||
goarm:
|
||||
- 6
|
||||
- 7
|
||||
archives:
|
||||
-
|
||||
replacements:
|
||||
|
|
41
README.md
41
README.md
|
@ -24,10 +24,9 @@ Paths:
|
|||
* Packages can specified in at the beginning of a path with a `:` seperator.
|
||||
github.com/markbates/pkger:/cmd/pkger/main.go
|
||||
* 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" => /go/pkg/mod/github.com/gobuffalo/buffalo@v0.14.7/go.mod
|
||||
"github.com/gobuffalo/buffalo:/go.mod" => $GOPATH/pkg/mod/github.com/gobuffalo/buffalo@v0.14.7/go.mod
|
||||
```
|
||||
|
||||
## CLI
|
||||
|
@ -54,7 +53,7 @@ package <.>
|
|||
// Pkger stuff here
|
||||
```
|
||||
|
||||
The `-o` flag can be used to specify the directory of the `pkged.go` file.
|
||||
The `-o` flag can be used specify the directory of the `pkged.go` file.
|
||||
|
||||
```bash
|
||||
$ pkger -o cmd/reader
|
||||
|
@ -285,26 +284,26 @@ $ pkger parse
|
|||
".": [
|
||||
{
|
||||
"file": {
|
||||
"Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz",
|
||||
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz",
|
||||
"Path": {
|
||||
"Pkg": "app",
|
||||
"Name": "/foo/bar/baz"
|
||||
},
|
||||
"Here": {
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"ImportPath": "app",
|
||||
"Module": {
|
||||
"Path": "app",
|
||||
"Main": true,
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"GoVersion": "1.13"
|
||||
},
|
||||
"Name": "main"
|
||||
}
|
||||
},
|
||||
"pos": {
|
||||
"Filename": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
|
||||
"Filename": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
|
||||
"Offset": 629,
|
||||
"Line": 47,
|
||||
"Column": 27
|
||||
|
@ -314,26 +313,26 @@ $ pkger parse
|
|||
},
|
||||
{
|
||||
"file": {
|
||||
"Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz/biz.txt",
|
||||
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz/biz.txt",
|
||||
"Path": {
|
||||
"Pkg": "app",
|
||||
"Name": "/foo/bar/baz/biz.txt"
|
||||
},
|
||||
"Here": {
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"ImportPath": "app",
|
||||
"Module": {
|
||||
"Path": "app",
|
||||
"Main": true,
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"GoVersion": "1.13"
|
||||
},
|
||||
"Name": "main"
|
||||
}
|
||||
},
|
||||
"pos": {
|
||||
"Filename": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
|
||||
"Filename": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
|
||||
"Offset": 706,
|
||||
"Line": 51,
|
||||
"Column": 25
|
||||
|
@ -389,38 +388,38 @@ $ pkger list -json
|
|||
"ImportPath": "app",
|
||||
"Files": [
|
||||
{
|
||||
"Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
|
||||
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
|
||||
"Path": {
|
||||
"Pkg": "app",
|
||||
"Name": "/assets"
|
||||
},
|
||||
"Here": {
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
|
||||
"ImportPath": "",
|
||||
"Module": {
|
||||
"Path": "app",
|
||||
"Main": true,
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"GoVersion": "1.13"
|
||||
},
|
||||
"Name": "assets"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets/css",
|
||||
"Abs": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets/css",
|
||||
"Path": {
|
||||
"Pkg": "app",
|
||||
"Name": "/assets/css"
|
||||
},
|
||||
"Here": {
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
|
||||
"ImportPath": "",
|
||||
"Module": {
|
||||
"Path": "app",
|
||||
"Main": true,
|
||||
"Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"Dir": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
|
||||
"GoMod": "$GOPATH/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
|
||||
"GoVersion": "1.13"
|
||||
},
|
||||
"Name": "assets"
|
||||
|
|
|
@ -9,6 +9,8 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -69,7 +71,15 @@ func (e *listCmd) Exec(args []string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = os.Stdout.Write(bb.Bytes())
|
||||
hep := hepa.New()
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -124,11 +124,6 @@ func (e *packCmd) Flags() *flag.FlagSet {
|
|||
}
|
||||
|
||||
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)
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
|
@ -142,12 +137,14 @@ func Package(info here.Info, out string, decls parser.Decls) error {
|
|||
}
|
||||
defer f.Close()
|
||||
|
||||
fmt.Fprintf(f, "// Code generated by pkger; DO NOT EDIT.\n\n")
|
||||
fmt.Fprintf(f, "// +build !skippkger\n\n")
|
||||
c, err := here.Dir(filepath.Dir(out))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(f, "package %s\n\n", c.Name)
|
||||
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, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`")
|
||||
fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n")
|
||||
fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n")
|
||||
fmt.Fprintf(f, "\nvar _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`")
|
||||
|
||||
if err := pkgutil.Stuff(f, info, decls); err != nil {
|
||||
return err
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
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/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/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
|
@ -23,4 +19,3 @@ 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/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
5
go.mod
|
@ -3,6 +3,9 @@ module github.com/markbates/pkger
|
|||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/gobuffalo/here v0.6.0
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
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
7
go.sum
|
@ -1,8 +1,6 @@
|
|||
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/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/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
|
@ -10,13 +8,12 @@ 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/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 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||
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/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=
|
||||
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
|
||||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
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)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
51
here/here.go
51
here/here.go
|
@ -1,14 +1,49 @@
|
|||
package here
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/here"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Info = here.Info
|
||||
type Module = here.Module
|
||||
type Path = here.Path
|
||||
var cache = &infoMap{
|
||||
data: &sync.Map{},
|
||||
}
|
||||
|
||||
var Here = here.New()
|
||||
var Dir = Here.Dir
|
||||
var Package = Here.Package
|
||||
var Current = Here.Current
|
||||
func run(n string, args ...string) ([]byte, error) {
|
||||
c := exec.Command(n, args...)
|
||||
|
||||
bb := &bytes.Buffer{}
|
||||
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`)
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
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)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
// 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
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
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()
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
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("([^:]+)(:(/.+))?")
|
|
@ -0,0 +1,63 @@
|
|||
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)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
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)
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
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
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
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.
|
|
@ -0,0 +1,35 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
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
|
||||
}()
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
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
|
@ -0,0 +1,29 @@
|
|||
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",
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
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{}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package hepa
|
||||
|
||||
// Version of hepa
|
||||
const Version = "v0.0.1"
|
|
@ -40,7 +40,7 @@ func (decls Decls) Files() ([]*File, error) {
|
|||
|
||||
files, err := fl.Files(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %s", err, d)
|
||||
return nil, fmt.Errorf("%w: %s", err, d)
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
|
|
|
@ -5,9 +5,6 @@ import (
|
|||
"fmt"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
)
|
||||
|
||||
var _ Decl = IncludeDecl{}
|
||||
|
@ -18,34 +15,6 @@ type IncludeDecl struct {
|
|||
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 {
|
||||
return fmt.Sprintf("pkger.Include(%q)", d.value)
|
||||
}
|
||||
|
|
|
@ -100,22 +100,19 @@ func (p *Parser) ParseDir(abs string, mode parser.Mode) ([]*ParsedSource, error)
|
|||
|
||||
her, err := here.Dir(abs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: here.Dir failed %s", err, abs)
|
||||
return nil, fmt.Errorf("%w: here.Dir failed %s", err, abs)
|
||||
}
|
||||
|
||||
pt, err := her.Parse(strings.TrimPrefix(abs, filepath.Dir(abs)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: here.Parse failed %s", err, abs)
|
||||
}
|
||||
|
||||
filter := func(f os.FileInfo) bool {
|
||||
return !f.IsDir()
|
||||
return nil, fmt.Errorf("%w: here.Parse failed %s", err, abs)
|
||||
}
|
||||
|
||||
fset := token.NewFileSet()
|
||||
pkgs, err := parser.ParseDir(fset, abs, filter, 0)
|
||||
|
||||
pkgs, err := parser.ParseDir(fset, abs, nil, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: ParseDir failed %s", err, abs)
|
||||
return nil, fmt.Errorf("%w: ParseDir failed %s", err, abs)
|
||||
}
|
||||
|
||||
var srcs []*ParsedSource
|
||||
|
@ -208,13 +205,13 @@ func (p *Parser) parse() error {
|
|||
|
||||
srcs, err := p.ParseDir(path, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %s", err, path)
|
||||
return fmt.Errorf("%w: %s", err, path)
|
||||
}
|
||||
|
||||
for _, src := range srcs {
|
||||
mm, err := src.DeclsMap()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %s", err, src.Abs)
|
||||
return fmt.Errorf("%w: %s", err, src.Abs)
|
||||
}
|
||||
for k, v := range mm {
|
||||
p.decls[k] = append(p.decls[k], v...)
|
||||
|
@ -229,12 +226,30 @@ func (p *Parser) parse() error {
|
|||
|
||||
func (p *Parser) parseIncludes() error {
|
||||
for _, i := range p.includes {
|
||||
d, err := NewInclude(p.Info, i)
|
||||
pt, err := p.Info.Parse(i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.decls["Include"] = append(p.decls["Include"], d)
|
||||
her := p.Info
|
||||
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
|
||||
}
|
||||
|
|
|
@ -41,6 +41,14 @@ func Test_Parser_Ref(t *testing.T) {
|
|||
fmt.Println(f.Path)
|
||||
}
|
||||
r.Len(files, 25)
|
||||
|
||||
for _, f := range files {
|
||||
if f.Path.Pkg == ref.Module.Path {
|
||||
r.True(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir)
|
||||
} else {
|
||||
r.False(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Parser_Ref_Include(t *testing.T) {
|
||||
|
@ -50,6 +58,7 @@ func Test_Parser_Ref_Include(t *testing.T) {
|
|||
}()
|
||||
r := require.New(t)
|
||||
|
||||
here.ClearCache()
|
||||
ref, err := pkgtest.NewRef()
|
||||
r.NoError(err)
|
||||
defer os.RemoveAll(ref.Dir)
|
||||
|
@ -66,35 +75,25 @@ func Test_Parser_Ref_Include(t *testing.T) {
|
|||
|
||||
files, err := res.Files()
|
||||
r.NoError(err)
|
||||
// t.FailNow()
|
||||
|
||||
l := len(files)
|
||||
r.Equal(26, l)
|
||||
}
|
||||
// r.Len(files, 27)
|
||||
|
||||
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))
|
||||
for _, f := range files {
|
||||
if f.Path.Pkg == ref.Module.Path {
|
||||
r.True(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir)
|
||||
} else {
|
||||
r.False(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Parser_Example_HTTP(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
here.ClearCache()
|
||||
cur, err := here.Package("github.com/markbates/pkger")
|
||||
r.NoError(err)
|
||||
|
||||
|
|
|
@ -33,39 +33,16 @@ func (p *ParsedSource) Parse() error {
|
|||
return p.err
|
||||
}
|
||||
|
||||
func (p *ParsedSource) valueIdent(node *ast.Ident) (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 {
|
||||
func (p *ParsedSource) value(node ast.Node) (string, error) {
|
||||
var s string
|
||||
|
||||
switch x := node.(type) {
|
||||
case *ast.BasicLit:
|
||||
s = x.Value
|
||||
case *ast.Ident:
|
||||
s = p.valueIdent(x)
|
||||
s = x.Name
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (p *ParsedSource) value(node ast.Node) (string, error) {
|
||||
s := p.valueNode(node)
|
||||
return strconv.Unquote(s)
|
||||
}
|
||||
|
||||
|
@ -163,26 +140,26 @@ func (p *ParsedSource) parse() error {
|
|||
n := ce.Args[0]
|
||||
val, err := p.value(n)
|
||||
if err != nil {
|
||||
p.err = fmt.Errorf("%s: %s", err, n)
|
||||
p.err = fmt.Errorf("%w: %s", err, n)
|
||||
return false
|
||||
}
|
||||
|
||||
info, err := here.Dir(filepath.Dir(p.Abs))
|
||||
if err != nil {
|
||||
p.err = fmt.Errorf("%s: %s", err, p.Abs)
|
||||
p.err = fmt.Errorf("%w: %s", err, p.Abs)
|
||||
return false
|
||||
}
|
||||
|
||||
pt, err := info.Parse(val)
|
||||
if err != nil {
|
||||
p.err = fmt.Errorf("%s: %s", err, p.Abs)
|
||||
p.err = fmt.Errorf("%w: %s", err, p.Abs)
|
||||
return false
|
||||
}
|
||||
|
||||
if pt.Pkg != info.Module.Path {
|
||||
info, err = here.Package(pt.Pkg)
|
||||
if err != nil {
|
||||
p.err = fmt.Errorf("%s: %s", err, p.Abs)
|
||||
p.err = fmt.Errorf("%w: %s", err, p.Abs)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"io"
|
||||
|
||||
"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) {
|
||||
|
@ -44,7 +46,16 @@ func Encode(b []byte) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
s := hex.EncodeToString(bb.Bytes())
|
||||
hep := hepa.New()
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ func (fx *Pkger) Add(files ...*os.File) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pt, err := fx.Parse(f.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -21,6 +21,7 @@ func NewRef() (*Ref, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
root := filepath.Join(
|
||||
her.Module.Dir,
|
||||
"pkging",
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
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/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/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
|
@ -22,4 +18,3 @@ 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/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=
|
||||
|
|
|
@ -18,16 +18,12 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
const (
|
||||
unused, pathAsset = "", "/assets"
|
||||
)
|
||||
|
||||
func run() error {
|
||||
if err := actions.WalkTemplates(os.Stdout); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := pkger.Walk(pathAsset, func(path string, info os.FileInfo, err error) error {
|
||||
err := pkger.Walk("/assets", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func Serve() {
|
||||
pkger.Stat("github.com/markbates/pkger:/README.md")
|
||||
pkger.Stat("github.com/stretchr/testify:/README.md")
|
||||
dir := http.FileServer(pkger.Dir("/public"))
|
||||
http.ListenAndServe(":3000", dir)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
package pkgutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/parser"
|
||||
|
@ -25,12 +23,9 @@ func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
|
|||
|
||||
for _, pf := range files {
|
||||
err = func() error {
|
||||
if strings.HasSuffix(pf.Abs, ".tmp") {
|
||||
return nil
|
||||
}
|
||||
df, err := os.Open(pf.Abs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could open stuff %s: %s", pf.Abs, err)
|
||||
return err
|
||||
}
|
||||
defer df.Close()
|
||||
|
||||
|
|
|
@ -19,12 +19,6 @@ func Test_Stuff(t *testing.T) {
|
|||
r.NoError(err)
|
||||
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)
|
||||
r.NoError(err)
|
||||
|
||||
|
|
|
@ -35,17 +35,16 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
her, err := fx.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
name = filepath.Join(her.Dir, pt.Name)
|
||||
name = filepath.Join(fx.Here.Dir, pt.Name)
|
||||
f, err := os.Create(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
her, err := fx.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nf := &File{
|
||||
File: f,
|
||||
her: her,
|
||||
|
@ -83,15 +82,7 @@ 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.
|
||||
func (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
|
||||
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)
|
||||
return os.MkdirAll(filepath.Join(f.Here.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.
|
||||
|
@ -101,13 +92,13 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
her, err := fx.Info(pt.Pkg)
|
||||
name = filepath.Join(fx.Here.Dir, pt.Name)
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
name = filepath.Join(her.Dir, pt.Name)
|
||||
f, err := os.Open(name)
|
||||
her, err := fx.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -140,12 +131,7 @@ func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
her, err := fx.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info, err := os.Stat(filepath.Join(her.Dir, pt.Name))
|
||||
info, err := os.Stat(filepath.Join(fx.Here.Dir, pt.Name))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -162,27 +148,19 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
|
|||
return err
|
||||
}
|
||||
|
||||
info, err := f.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fp := filepath.Join(info.Dir, pt.Name)
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
path = strings.TrimPrefix(path, info.Dir)
|
||||
path = strings.ReplaceAll(path, "\\", "/")
|
||||
pt.Name = path
|
||||
|
@ -198,13 +176,7 @@ func (fx *Pkger) Remove(name string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
info, err := fx.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Remove(filepath.Join(info.Dir, pt.Name))
|
||||
return os.Remove(filepath.Join(fx.Here.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).
|
||||
|
@ -213,11 +185,5 @@ func (fx *Pkger) RemoveAll(name string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
info, err := fx.Info(pt.Pkg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.RemoveAll(filepath.Join(info.Dir, pt.Name))
|
||||
return os.RemoveAll(filepath.Join(fx.Here.Dir, pt.Name))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue