forked from mirror/pkger
commit
b03c3dba90
4
Makefile
4
Makefile
|
@ -1,10 +1,14 @@
|
|||
TAGS ?= ""
|
||||
GO_BIN ?= "go"
|
||||
|
||||
|
||||
install: tidy
|
||||
cd ./cmd/pkger && $(GO_BIN) install -tags ${TAGS} -v .
|
||||
make tidy
|
||||
|
||||
run: install
|
||||
cd ./examples/app; pkger
|
||||
|
||||
tidy:
|
||||
$(GO_BIN) mod tidy -v
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package pkger
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/markbates/pkger/pkging/pkgutil"
|
||||
)
|
||||
|
||||
var current pkging.Pkger
|
||||
|
||||
func Apply(pkg pkging.Pkger, err error) error {
|
||||
if err := pkgutil.Dump(os.Stdout, pkg); err != nil {
|
||||
return err
|
||||
}
|
||||
current = pkging.Wrap(current, pkg)
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package pkger
|
|
@ -31,4 +31,4 @@ func run() error {
|
|||
return root.Route(os.Args[1:])
|
||||
}
|
||||
|
||||
// does not computee
|
||||
// does not compute
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/markbates/pkger"
|
||||
"github.com/markbates/pkger/parser"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/markbates/pkger/stuffing"
|
||||
)
|
||||
|
||||
const outName = "pkged.go"
|
||||
|
@ -124,6 +125,7 @@ func Package(out string, paths []pkging.Path) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
c, err := pkger.Current()
|
||||
if err != nil {
|
||||
|
@ -131,14 +133,15 @@ func Package(out string, paths []pkging.Path) error {
|
|||
}
|
||||
fmt.Fprintf(f, "package %s\n\n", c.Name)
|
||||
fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n")
|
||||
fmt.Fprintf(f, "var _ = pkger.Unpack(`")
|
||||
fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n")
|
||||
|
||||
// TODO
|
||||
// if err := pkger.Pack(f, paths); err != nil {
|
||||
// return err
|
||||
// }
|
||||
fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`")
|
||||
|
||||
fmt.Fprintf(f, "`)\n")
|
||||
if err := stuffing.Stuff(f, c, paths); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(f, "`)))\n")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
)
|
||||
|
||||
func walk(args []string) error {
|
||||
err := pkger.Walk(".", func(path string, info os.FileInfo, err error) error {
|
||||
err := pkger.Walk("/", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
example
|
||||
app
|
||||
pkged.go
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
default:
|
||||
cd ../../../cmd/pkger && go install -v .
|
||||
cd ../../cmd/pkger && go install -v .
|
||||
pkger
|
||||
GOOS=linux go build -v -o example
|
||||
docker build -t pkger:example .
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
@ -15,7 +8,5 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
|
|||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
|
3
go.mod
3
go.mod
|
@ -3,9 +3,6 @@ module github.com/markbates/pkger
|
|||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/markbates/errx v1.1.0
|
||||
github.com/stretchr/testify v1.4.0
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
)
|
||||
|
|
9
go.sum
9
go.sum
|
@ -1,12 +1,5 @@
|
|||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI=
|
||||
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
@ -16,7 +9,5 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
|
|||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
|
12
here/dir.go
12
here/dir.go
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
// Dir attempts to gather info for the requested directory.
|
||||
func Dir(p string) (Info, error) {
|
||||
return Cache(p, func(p string) (Info, error) {
|
||||
i, err := Cache(p, func(p string) (Info, error) {
|
||||
var i Info
|
||||
|
||||
fi, err := os.Stat(p)
|
||||
|
@ -40,4 +40,14 @@ func Dir(p string) (Info, error) {
|
|||
|
||||
return i, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return i, err
|
||||
}
|
||||
|
||||
Cache(i.ImportPath, func(p string) (Info, error) {
|
||||
return i, nil
|
||||
})
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
|
13
here/pkg.go
13
here/pkg.go
|
@ -14,7 +14,7 @@ import (
|
|||
// returned `Info` value and pass it to the `Dir(string) (Info, error)`
|
||||
// function to return the complete data.
|
||||
func Package(p string) (Info, error) {
|
||||
return Cache(p, func(p string) (Info, error) {
|
||||
i, err := Cache(p, func(p string) (Info, error) {
|
||||
var i Info
|
||||
b, err := run("go", "list", "-json", "-find", p)
|
||||
if err != nil {
|
||||
|
@ -26,4 +26,15 @@ func Package(p string) (Info, error) {
|
|||
|
||||
return i, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return i, err
|
||||
}
|
||||
|
||||
Cache(i.Dir, func(p string) (Info, error) {
|
||||
return i, nil
|
||||
})
|
||||
|
||||
return i, nil
|
||||
|
||||
}
|
||||
|
|
|
@ -14,10 +14,10 @@ import (
|
|||
|
||||
var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"}
|
||||
|
||||
func Parse(cur here.Info) (Results, error) {
|
||||
func Parse(her here.Info) (Results, error) {
|
||||
var r Results
|
||||
|
||||
name := cur.ImportPath
|
||||
name := her.ImportPath
|
||||
|
||||
pt, err := pkger.Parse(name)
|
||||
if err != nil {
|
||||
|
@ -25,12 +25,6 @@ func Parse(cur here.Info) (Results, error) {
|
|||
}
|
||||
r.Path = pt
|
||||
|
||||
her, err := pkger.Info(r.Path.Pkg)
|
||||
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
|
||||
m := map[pkging.Path]bool{}
|
||||
|
||||
root := r.Path.Name
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"testing"
|
||||
|
@ -12,7 +13,9 @@ import (
|
|||
func Test_Parser(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
ch := filepath.Join("..", "examples", "app")
|
||||
ch := filepath.Join("..",
|
||||
"examples",
|
||||
"app")
|
||||
info := here.Info{
|
||||
Dir: ch,
|
||||
ImportPath: "github.com/markbates/pkger/examples/app",
|
||||
|
@ -24,15 +27,12 @@ func Test_Parser(t *testing.T) {
|
|||
exp := []string{
|
||||
"github.com/markbates/pkger/examples/app:/",
|
||||
"github.com/markbates/pkger/examples/app:/public",
|
||||
"github.com/markbates/pkger/examples/app:/templates",
|
||||
"github.com/markbates/pkger/examples/app:/templates/a.txt",
|
||||
"github.com/markbates/pkger/examples/app:/templates/b",
|
||||
"github.com/markbates/pkger/examples/app:/public/images/mark-small.png",
|
||||
"github.com/markbates/pkger/examples/app:/public/images",
|
||||
"github.com/markbates/pkger/examples/app:/public/images/mark.png",
|
||||
"github.com/markbates/pkger/examples/app:/public/images/mark_250px.png",
|
||||
"github.com/markbates/pkger/examples/app:/public/images/mark_400px.png",
|
||||
"github.com/markbates/pkger/examples/app:/public/index.html",
|
||||
"github.com/markbates/pkger/examples/app:/templates/a.txt",
|
||||
}
|
||||
sort.Strings(exp)
|
||||
|
||||
|
@ -42,5 +42,6 @@ func Test_Parser(t *testing.T) {
|
|||
}
|
||||
|
||||
sort.Strings(act)
|
||||
fmt.Printf("%#v\n", act)
|
||||
r.Equal(exp, act)
|
||||
}
|
||||
|
|
78
pkger.go
78
pkger.go
|
@ -1,29 +1,69 @@
|
|||
package pkger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/markbates/pkger/pkging/stdos"
|
||||
)
|
||||
|
||||
var current = func() pkging.Pkger {
|
||||
n, err := stdos.New()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return n
|
||||
var _ = func() error {
|
||||
return Apply(stdos.New())
|
||||
}()
|
||||
|
||||
var Abs = current.Abs
|
||||
var AbsPath = current.AbsPath
|
||||
var Create = current.Create
|
||||
var Current = current.Current
|
||||
var Info = current.Info
|
||||
var MkdirAll = current.MkdirAll
|
||||
var Open = current.Open
|
||||
var Parse = current.Parse
|
||||
var Remove = current.Remove
|
||||
var RemoveAll = current.RemoveAll
|
||||
var Stat = current.Stat
|
||||
var Walk = current.Walk
|
||||
func Parse(p string) (pkging.Path, error) {
|
||||
return current.Parse(p)
|
||||
}
|
||||
|
||||
func Abs(p string) (string, error) {
|
||||
return current.Abs(p)
|
||||
}
|
||||
|
||||
func AbsPath(p pkging.Path) (string, error) {
|
||||
return current.AbsPath(p)
|
||||
}
|
||||
|
||||
func Current() (here.Info, error) {
|
||||
return current.Current()
|
||||
}
|
||||
|
||||
func Info(p string) (here.Info, error) {
|
||||
return current.Info(p)
|
||||
}
|
||||
|
||||
// Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.
|
||||
func Create(p string) (pkging.File, error) {
|
||||
return current.Create(p)
|
||||
}
|
||||
|
||||
// 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 MkdirAll(p string, perm os.FileMode) error {
|
||||
return current.MkdirAll(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.
|
||||
func Open(p string) (pkging.File, error) {
|
||||
return current.Open(p)
|
||||
}
|
||||
|
||||
// Stat returns a FileInfo describing the named file.
|
||||
func Stat(name string) (os.FileInfo, error) {
|
||||
return current.Stat(name)
|
||||
}
|
||||
|
||||
// Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links. - That is from the standard library. I know. Their grammar teachers can not be happy with them right now.
|
||||
func Walk(p string, wf filepath.WalkFunc) error {
|
||||
return current.Walk(p, wf)
|
||||
}
|
||||
|
||||
// Remove removes the named file or (empty) directory.
|
||||
func Remove(name string) error {
|
||||
return current.Remove(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).
|
||||
func RemoveAll(name string) error {
|
||||
return current.RemoveAll(name)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package pkger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Parse(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
pt, err := Parse("github.com/rocket/ship:/little")
|
||||
r.NoError(err)
|
||||
r.Equal("github.com/rocket/ship", pt.Pkg)
|
||||
r.Equal("/little", pt.Name)
|
||||
}
|
||||
|
||||
func Test_Abs(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
s, err := Abs(":/rocket.ship")
|
||||
r.NoError(err)
|
||||
|
||||
pwd, err := os.Getwd()
|
||||
r.NoError(err)
|
||||
r.Equal(filepath.Join(pwd, "rocket.ship"), s)
|
||||
}
|
||||
|
||||
func Test_AbsPath(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
s, err := AbsPath(pkging.Path{
|
||||
Pkg: "github.com/markbates/pkger",
|
||||
Name: "/rocket.ship",
|
||||
})
|
||||
r.NoError(err)
|
||||
|
||||
pwd, err := os.Getwd()
|
||||
r.NoError(err)
|
||||
r.Equal(filepath.Join(pwd, "rocket.ship"), s)
|
||||
}
|
|
@ -65,26 +65,18 @@ func NewFileInfo(info os.FileInfo) *FileInfo {
|
|||
}
|
||||
|
||||
func WithName(name string, info os.FileInfo) *FileInfo {
|
||||
s := cleanName(name)
|
||||
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
fo := NewFileInfo(info)
|
||||
fo.Details.Name = cleanName(name)
|
||||
return fo
|
||||
}
|
||||
|
||||
func WithRelName(name string, info os.FileInfo) *FileInfo {
|
||||
s := cleanName(name)
|
||||
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
fo := NewFileInfo(info)
|
||||
fo.Details.Name = cleanName(name)
|
||||
|
||||
s := cleanName(name)
|
||||
s = strings.TrimPrefix(s, "/")
|
||||
|
||||
fo.Details.Name = s
|
||||
return fo
|
||||
}
|
||||
|
||||
|
@ -92,5 +84,8 @@ func cleanName(s string) string {
|
|||
if strings.Contains(s, "\\") {
|
||||
s = strings.Replace(s, "\\", "/", -1)
|
||||
}
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package pkging
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_NewFileInfo(t *testing.T) {
|
||||
|
||||
in := []string{
|
||||
"/public/images/mark.png",
|
||||
"public/images/mark.png",
|
||||
"/public\\images/mark.png",
|
||||
"public/images\\mark.png",
|
||||
"\\public\\images\\mark.png",
|
||||
"public\\images\\mark.png",
|
||||
"\\public/images\\mark.png",
|
||||
"public\\images/mark.png",
|
||||
"\\public\\images\\mark.png",
|
||||
}
|
||||
|
||||
const exp = "/public/images/mark.png"
|
||||
for _, n := range in {
|
||||
t.Run(n, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
||||
f1 := &FileInfo{
|
||||
Details: Details{
|
||||
Name: n,
|
||||
Size: 42,
|
||||
Mode: os.FileMode(0644),
|
||||
IsDir: true,
|
||||
},
|
||||
}
|
||||
|
||||
f2 := NewFileInfo(f1)
|
||||
|
||||
r.Equal(exp, f2.Name())
|
||||
r.Equal(f1.Size(), f2.Size())
|
||||
r.Equal(f1.Mode(), f2.Mode())
|
||||
r.Equal(f1.IsDir(), f2.IsDir())
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_WithName(t *testing.T) {
|
||||
|
||||
f1 := &FileInfo{
|
||||
Details: Details{
|
||||
Name: "/walls/crumbling",
|
||||
Size: 42,
|
||||
Mode: os.FileMode(0644),
|
||||
IsDir: true,
|
||||
},
|
||||
}
|
||||
|
||||
const exp = "/public/images/mark.png"
|
||||
in := []string{
|
||||
"/public/images/mark.png",
|
||||
"public/images/mark.png",
|
||||
"/public\\images/mark.png",
|
||||
"public/images\\mark.png",
|
||||
"\\public\\images\\mark.png",
|
||||
"public\\images\\mark.png",
|
||||
"\\public/images\\mark.png",
|
||||
"public\\images/mark.png",
|
||||
"\\public\\images\\mark.png",
|
||||
}
|
||||
|
||||
for _, n := range in {
|
||||
t.Run(n, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
||||
f2 := WithName(n, f1)
|
||||
|
||||
r.Equal(exp, f2.Name())
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package mem
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func (pkg *Pkger) MarshalEmbed() ([]byte, error) {
|
||||
bb := &bytes.Buffer{}
|
||||
gz := gzip.NewWriter(bb)
|
||||
defer gz.Close()
|
||||
if err := json.NewEncoder(gz).Encode(pkg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := gz.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := hex.EncodeToString(bb.Bytes())
|
||||
return []byte(s), nil
|
||||
}
|
||||
|
||||
func UnmarshalEmbed(in []byte) (pkging.Pkger, error) {
|
||||
b := make([]byte, len(in))
|
||||
if _, err := hex.Decode(b, in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gz, err := gzip.NewReader(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer gz.Close()
|
||||
|
||||
pkg := &Pkger{}
|
||||
if err := json.NewDecoder(gz).Decode(pkg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pkg, nil
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package mem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Pkger_MarshalEmbed(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
info, err := here.Current()
|
||||
r.NoError(err)
|
||||
|
||||
pkg, err := New(info)
|
||||
r.NoError(err)
|
||||
|
||||
const N = 10
|
||||
for i := 0; i < N; i++ {
|
||||
name := fmt.Sprintf("/%d.txt", i)
|
||||
f, err := pkg.Create(name)
|
||||
r.NoError(err)
|
||||
f.Write([]byte(name))
|
||||
r.NoError(f.Close())
|
||||
}
|
||||
|
||||
em, err := pkg.MarshalEmbed()
|
||||
r.NoError(err)
|
||||
|
||||
p2, err := UnmarshalEmbed(em)
|
||||
r.NoError(err)
|
||||
|
||||
pinfo, err := p2.Current()
|
||||
r.NoError(err)
|
||||
r.Equal(info, pinfo)
|
||||
|
||||
var act []os.FileInfo
|
||||
err = p2.Walk("/", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
act = append(act, info)
|
||||
|
||||
return nil
|
||||
})
|
||||
r.NoError(err)
|
||||
r.Len(act, N+1) // +1 for the / dir
|
||||
}
|
|
@ -2,6 +2,7 @@ package mem
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -30,6 +31,37 @@ type File struct {
|
|||
pkging pkging.Pkger
|
||||
}
|
||||
|
||||
type fJay struct {
|
||||
Info *pkging.FileInfo `json:"info"`
|
||||
Her here.Info `json:"her"`
|
||||
Path pkging.Path `json:"path"`
|
||||
Data []byte `json:"data"`
|
||||
Parent pkging.Path `json:"parent"`
|
||||
}
|
||||
|
||||
func (f File) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(fJay{
|
||||
Info: f.info,
|
||||
Her: f.her,
|
||||
Path: f.path,
|
||||
Data: f.data,
|
||||
Parent: f.parent,
|
||||
})
|
||||
}
|
||||
|
||||
func (f *File) UnmarshalJSON(b []byte) error {
|
||||
var y fJay
|
||||
if err := json.Unmarshal(b, &y); err != nil {
|
||||
return err
|
||||
}
|
||||
f.info = y.Info
|
||||
f.her = y.Her
|
||||
f.path = y.Path
|
||||
f.data = y.Data
|
||||
f.parent = y.Parent
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
|
||||
if sk, ok := f.reader.(io.Seeker); ok {
|
||||
return sk.Seek(ofpkginget, whence)
|
||||
|
@ -142,6 +174,10 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|||
return io.EOF
|
||||
}
|
||||
|
||||
if root == path {
|
||||
return nil
|
||||
}
|
||||
|
||||
pt, err := f.pkging.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -205,17 +241,6 @@ func (f *File) Open(name string) (http.File, error) {
|
|||
return di, nil
|
||||
}
|
||||
|
||||
// func (f File) MarshalJSON() ([]byte, error) {
|
||||
// m := map[string]interface{}{
|
||||
// "info": f.info,
|
||||
// "her": f.her,
|
||||
// "path": f.path,
|
||||
// "data": f.data,
|
||||
// "parent": f.parent,
|
||||
// }
|
||||
// return json.Marshal(m)
|
||||
// }
|
||||
|
||||
// func (f *File) UnmarshalJSON(b []byte) error {
|
||||
// m := map[string]json.RawMessage{}
|
||||
// if err := json.Unmarshal(b, &m); err != nil {
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package mem
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -41,6 +44,58 @@ type Pkger struct {
|
|||
current here.Info
|
||||
}
|
||||
|
||||
type jay struct {
|
||||
Infos *maps.Infos `json:"infos"`
|
||||
Paths *maps.Paths `json:"paths"`
|
||||
Files map[string]*File `json:"files"`
|
||||
Current here.Info `json:"current"`
|
||||
}
|
||||
|
||||
func (p *Pkger) MarshalJSON() ([]byte, error) {
|
||||
files := map[string]*File{}
|
||||
|
||||
p.files.Range(func(key pkging.Path, file pkging.File) bool {
|
||||
f, ok := file.(*File)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
files[key.String()] = f
|
||||
return true
|
||||
})
|
||||
|
||||
return json.Marshal(jay{
|
||||
Infos: p.infos,
|
||||
Paths: p.paths,
|
||||
Files: files,
|
||||
Current: p.current,
|
||||
})
|
||||
}
|
||||
|
||||
func (p *Pkger) UnmarshalJSON(b []byte) error {
|
||||
y := jay{}
|
||||
|
||||
if err := json.Unmarshal(b, &y); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.current = y.Current
|
||||
|
||||
p.infos = y.Infos
|
||||
|
||||
p.paths = y.Paths
|
||||
p.paths.Current = p.current
|
||||
|
||||
p.files = &maps.Files{}
|
||||
for k, v := range y.Files {
|
||||
pt, err := p.Parse(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.files.Store(pt, v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Pkger) Abs(p string) (string, error) {
|
||||
pt, err := f.Parse(p)
|
||||
if err != nil {
|
||||
|
@ -106,6 +161,33 @@ func (fx *Pkger) RemoveAll(name string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (fx *Pkger) Add(info os.FileInfo, r io.Reader) error {
|
||||
dir := filepath.Dir(info.Name())
|
||||
fx.MkdirAll(dir, 0755)
|
||||
|
||||
f, err := fx.Create(info.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mf, ok := f.(*File)
|
||||
if !ok {
|
||||
return fmt.Errorf("could not add %T", f)
|
||||
}
|
||||
|
||||
mf.info = pkging.NewFileInfo(info)
|
||||
|
||||
if !mf.info.IsDir() {
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mf.data = b
|
||||
}
|
||||
fx.files.Store(f.Path(), f)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fx *Pkger) Create(name string) (pkging.File, error) {
|
||||
fx.MkdirAll("/", 0755)
|
||||
pt, err := fx.Parse(name)
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
type Pkger interface {
|
||||
Parse(p string) (Path, error)
|
||||
Abs(string) (string, error)
|
||||
Abs(p string) (string, error)
|
||||
AbsPath(Path) (string, error)
|
||||
|
||||
Current() (here.Info, error)
|
||||
|
|
|
@ -2,6 +2,7 @@ package pkgtest
|
|||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/pkging/pkgutil"
|
||||
|
@ -48,9 +49,61 @@ func (s Suite) Test_File_Info(t *testing.T) {
|
|||
// panic("not implemented")
|
||||
// }
|
||||
//
|
||||
// func (s Suite) Test_File_Readdir(t *testing.T) {
|
||||
// panic("not implemented")
|
||||
// }
|
||||
func (s Suite) Test_File_Readdir(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
pkg, err := s.Make()
|
||||
r.NoError(err)
|
||||
|
||||
cur, err := pkg.Current()
|
||||
r.NoError(err)
|
||||
|
||||
ip := cur.ImportPath
|
||||
table := []struct {
|
||||
in string
|
||||
}{
|
||||
{in: ":/public"},
|
||||
{in: ip + ":/public"},
|
||||
}
|
||||
|
||||
r.NoError(s.LoadFolder(pkg))
|
||||
|
||||
for _, tt := range table {
|
||||
s.Run(t, tt.in, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
||||
dir, err := pkg.Open(tt.in)
|
||||
r.NoError(err)
|
||||
defer dir.Close()
|
||||
|
||||
infos, err := dir.Readdir(-1)
|
||||
r.NoError(err)
|
||||
r.Len(infos, 2)
|
||||
|
||||
sort.Slice(infos, func(i, j int) bool {
|
||||
return infos[i].Name() < infos[j].Name()
|
||||
})
|
||||
|
||||
r.Equal("images", infos[0].Name())
|
||||
r.Equal("index.html", infos[1].Name())
|
||||
|
||||
dir, err = pkg.Open(tt.in + "/images")
|
||||
r.NoError(err)
|
||||
|
||||
infos, err = dir.Readdir(-1)
|
||||
r.NoError(err)
|
||||
r.Len(infos, 1)
|
||||
|
||||
sort.Slice(infos, func(i, j int) bool {
|
||||
return infos[i].Name() < infos[j].Name()
|
||||
})
|
||||
|
||||
r.Equal("mark.png", infos[0].Name())
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// func (s Suite) Test_File_Seek(t *testing.T) {
|
||||
// panic("not implemented")
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package pkgutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func Dump(w io.Writer, pkg pkging.Pkger) error {
|
||||
d := struct {
|
||||
Type string
|
||||
Info here.Info
|
||||
Paths map[string]os.FileInfo
|
||||
}{
|
||||
Type: fmt.Sprintf("%T", pkg),
|
||||
Paths: map[string]os.FileInfo{},
|
||||
}
|
||||
|
||||
info, err := pkg.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Info = info
|
||||
|
||||
err = pkg.Walk("/", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Paths[path] = info
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
if err := enc.Encode(d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -74,7 +74,7 @@ func (f *HTTP) Readdir(count int) ([]os.FileInfo, error) {
|
|||
|
||||
infos := make([]os.FileInfo, len(osinfos))
|
||||
for i, info := range osinfos {
|
||||
infos[i] = pkging.WithName(info.Name(), info)
|
||||
infos[i] = pkging.WithRelName(info.Name(), info)
|
||||
}
|
||||
return infos, err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
package pkging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
)
|
||||
|
||||
func Wrap(parent, with Pkger) Pkger {
|
||||
return withPkger{
|
||||
base: with,
|
||||
parent: parent,
|
||||
}
|
||||
}
|
||||
|
||||
type withPkger struct {
|
||||
base Pkger
|
||||
parent Pkger
|
||||
}
|
||||
|
||||
func (w withPkger) String() string {
|
||||
if w.parent == nil {
|
||||
return fmt.Sprintf("%T", w.base)
|
||||
}
|
||||
return fmt.Sprintf("%T > %T", w.base, w.parent)
|
||||
}
|
||||
|
||||
func (w withPkger) Parse(p string) (Path, error) {
|
||||
pt, err := w.base.Parse(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Parse(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
func (w withPkger) Abs(p string) (string, error) {
|
||||
pt, err := w.base.Abs(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Abs(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
func (w withPkger) AbsPath(p Path) (string, error) {
|
||||
pt, err := w.base.AbsPath(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.AbsPath(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
func (w withPkger) Current() (here.Info, error) {
|
||||
pt, err := w.base.Current()
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Current()
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
func (w withPkger) Info(p string) (here.Info, error) {
|
||||
pt, err := w.base.Info(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Info(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
// Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.
|
||||
func (w withPkger) Create(p string) (File, error) {
|
||||
pt, err := w.base.Create(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Create(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, 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 (w withPkger) MkdirAll(p string, perm os.FileMode) error {
|
||||
err := w.base.MkdirAll(p, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if w.parent != nil {
|
||||
return w.parent.MkdirAll(p, perm)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (w withPkger) Open(p string) (File, error) {
|
||||
pt, err := w.base.Open(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Open(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
// Stat returns a FileInfo describing the named file.
|
||||
func (w withPkger) Stat(p string) (os.FileInfo, error) {
|
||||
pt, err := w.base.Stat(p)
|
||||
if err != nil {
|
||||
if w.parent != nil {
|
||||
return w.parent.Stat(p)
|
||||
}
|
||||
return pt, err
|
||||
}
|
||||
return pt, nil
|
||||
}
|
||||
|
||||
// Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links. - That is from the standard library. I know. Their grammar teachers can not be happy with them right now.
|
||||
func (w withPkger) Walk(p string, wf filepath.WalkFunc) error {
|
||||
err := w.base.Walk(p, wf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if w.parent != nil {
|
||||
return w.parent.Walk(p, wf)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove removes the named file or (empty) directory.
|
||||
func (w withPkger) Remove(p string) error {
|
||||
err := w.base.Remove(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if w.parent != nil {
|
||||
return w.parent.Remove(p)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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).
|
||||
func (w withPkger) RemoveAll(p string) error {
|
||||
err := w.base.RemoveAll(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if w.parent != nil {
|
||||
return w.parent.RemoveAll(p)
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package stuffing
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/markbates/pkger/pkging/mem"
|
||||
"github.com/markbates/pkger/pkging/stdos"
|
||||
)
|
||||
|
||||
func Stuff(w io.Writer, cur here.Info, paths []pkging.Path) error {
|
||||
disk, err := stdos.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pkg, err := mem.New(cur)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, pt := range paths {
|
||||
err = func() error {
|
||||
f, err := disk.Open(pt.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := pkg.Add(fi, f); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
// WithInfo(ng, og)
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
b, err := pkg.MarshalEmbed()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(b)
|
||||
return err
|
||||
}
|
Loading…
Reference in New Issue