shine on me

This commit is contained in:
Mark Bates 2019-09-21 12:51:29 -04:00
parent f788ee9125
commit ae683c2648
21 changed files with 610 additions and 62 deletions

View File

@ -1,10 +1,14 @@
TAGS ?= "" TAGS ?= ""
GO_BIN ?= "go" GO_BIN ?= "go"
install: tidy install: tidy
cd ./cmd/pkger && $(GO_BIN) install -tags ${TAGS} -v . cd ./cmd/pkger && $(GO_BIN) install -tags ${TAGS} -v .
make tidy make tidy
run: install
cd ./examples/app; pkger
tidy: tidy:
$(GO_BIN) mod tidy -v $(GO_BIN) mod tidy -v

20
apply.go Normal file
View File

@ -0,0 +1,20 @@
package pkger
import (
"sync"
"github.com/markbates/pkger/pkging"
)
var current pkging.Pkger
var gil = &sync.RWMutex{}
func Apply(pkg pkging.Pkger, err error) error {
if err != nil {
return err
}
gil.Lock()
defer gil.Unlock()
current = pkging.Wrap(current, pkg)
return nil
}

View File

@ -31,4 +31,4 @@ func run() error {
return root.Route(os.Args[1:]) return root.Route(os.Args[1:])
} }
// does not computee // does not compute

View File

@ -9,6 +9,7 @@ import (
"github.com/markbates/pkger" "github.com/markbates/pkger"
"github.com/markbates/pkger/parser" "github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/stuffing"
) )
const outName = "pkged.go" const outName = "pkged.go"
@ -124,6 +125,7 @@ func Package(out string, paths []pkging.Path) error {
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
c, err := pkger.Current() c, err := pkger.Current()
if err != nil { 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, "package %s\n\n", c.Name)
fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n") 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 fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`")
// if err := pkger.Pack(f, paths); err != nil {
// return err
// }
fmt.Fprintf(f, "`)\n") if err := stuffing.Stuff(f, c, paths); err != nil {
return err
}
fmt.Fprintf(f, "`)))\n")
return nil return nil
} }

View File

@ -8,7 +8,7 @@ import (
) )
func walk(args []string) error { 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 { if err != nil {
return err return err
} }

View File

@ -1,2 +1,3 @@
example example
app app
pkged.go

View File

@ -1,5 +1,5 @@
default: default:
cd ../../../cmd/pkger && go install -v . cd ../../cmd/pkger && go install -v .
pkger pkger
GOOS=linux go build -v -o example GOOS=linux go build -v -o example
docker build -t pkger:example . docker build -t pkger:example .

3
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/kr/pretty v0.1.0 // indirect
github.com/markbates/errx v1.1.0 github.com/markbates/errx v1.1.0
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
) )

9
go.sum
View File

@ -1,12 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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.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 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI=
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= 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 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= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 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=

View File

@ -8,7 +8,7 @@ import (
// Dir attempts to gather info for the requested directory. // Dir attempts to gather info for the requested directory.
func Dir(p string) (Info, error) { 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 var i Info
fi, err := os.Stat(p) fi, err := os.Stat(p)
@ -40,4 +40,14 @@ func Dir(p string) (Info, error) {
return i, nil return i, nil
}) })
if err != nil {
return i, err
}
Cache(i.ImportPath, func(p string) (Info, error) {
return i, nil
})
return i, nil
} }

View File

@ -14,7 +14,7 @@ import (
// returned `Info` value and pass it to the `Dir(string) (Info, error)` // returned `Info` value and pass it to the `Dir(string) (Info, error)`
// function to return the complete data. // function to return the complete data.
func Package(p string) (Info, error) { 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 var i Info
b, err := run("go", "list", "-json", "-find", p) b, err := run("go", "list", "-json", "-find", p)
if err != nil { if err != nil {
@ -26,4 +26,15 @@ func Package(p string) (Info, error) {
return i, nil 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

@ -14,10 +14,10 @@ import (
var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"} 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 var r Results
name := cur.ImportPath name := her.ImportPath
pt, err := pkger.Parse(name) pt, err := pkger.Parse(name)
if err != nil { if err != nil {
@ -25,12 +25,6 @@ func Parse(cur here.Info) (Results, error) {
} }
r.Path = pt r.Path = pt
her, err := pkger.Info(r.Path.Pkg)
if err != nil {
return r, err
}
m := map[pkging.Path]bool{} m := map[pkging.Path]bool{}
root := r.Path.Name root := r.Path.Name

View File

@ -1,6 +1,7 @@
package parser package parser
import ( import (
"fmt"
"path/filepath" "path/filepath"
"sort" "sort"
"testing" "testing"
@ -12,7 +13,9 @@ import (
func Test_Parser(t *testing.T) { func Test_Parser(t *testing.T) {
r := require.New(t) r := require.New(t)
ch := filepath.Join("..", "examples", "app") ch := filepath.Join("..",
"examples",
"app")
info := here.Info{ info := here.Info{
Dir: ch, Dir: ch,
ImportPath: "github.com/markbates/pkger/examples/app", ImportPath: "github.com/markbates/pkger/examples/app",
@ -24,15 +27,12 @@ func Test_Parser(t *testing.T) {
exp := []string{ exp := []string{
"github.com/markbates/pkger/examples/app:/", "github.com/markbates/pkger/examples/app:/",
"github.com/markbates/pkger/examples/app:/public", "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/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.png",
"github.com/markbates/pkger/examples/app:/public/images/mark_250px.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/images/mark_400px.png",
"github.com/markbates/pkger/examples/app:/public/index.html", "github.com/markbates/pkger/examples/app:/public/index.html",
"github.com/markbates/pkger/examples/app:/templates/a.txt",
} }
sort.Strings(exp) sort.Strings(exp)
@ -42,5 +42,6 @@ func Test_Parser(t *testing.T) {
} }
sort.Strings(act) sort.Strings(act)
fmt.Printf("%#v\n", act)
r.Equal(exp, act) r.Equal(exp, act)
} }

133
pkger.go
View File

@ -2,28 +2,133 @@ package pkger
import ( import (
"log" "log"
"os"
"path/filepath"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/stdos" "github.com/markbates/pkger/pkging/stdos"
) )
var current = func() pkging.Pkger { var disk = func() pkging.Pkger {
n, err := stdos.New() n, err := stdos.New()
if err != nil { if err != nil {
log.Fatal(err) log.Println(err)
} }
return n return n
}() }()
var Abs = current.Abs func Parse(p string) (pkging.Path, error) {
var AbsPath = current.AbsPath gil.RLock()
var Create = current.Create defer gil.RUnlock()
var Current = current.Current if current == nil {
var Info = current.Info return disk.Parse(p)
var MkdirAll = current.MkdirAll }
var Open = current.Open return current.Parse(p)
var Parse = current.Parse }
var Remove = current.Remove
var RemoveAll = current.RemoveAll func Abs(p string) (string, error) {
var Stat = current.Stat gil.RLock()
var Walk = current.Walk defer gil.RUnlock()
if current == nil {
return disk.Abs(p)
}
return current.Abs(p)
}
func AbsPath(p pkging.Path) (string, error) {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.AbsPath(p)
}
return current.AbsPath(p)
}
func Current() (here.Info, error) {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Current()
}
return current.Current()
}
func Info(p string) (here.Info, error) {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Info(p)
}
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) {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Create(p)
}
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 {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.MkdirAll(p, perm)
}
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) {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Open(p)
}
return current.Open(p)
}
// Stat returns a FileInfo describing the named file.
func Stat(name string) (os.FileInfo, error) {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Stat(name)
}
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 {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Walk(p, wf)
}
return current.Walk(p, wf)
}
// Remove removes the named file or (empty) directory.
func Remove(name string) error {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.Remove(name)
}
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 {
gil.RLock()
defer gil.RUnlock()
if current == nil {
return disk.RemoveAll(name)
}
return current.RemoveAll(name)
}

44
pkging/mem/embed.go Normal file
View File

@ -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
}

51
pkging/mem/embed_test.go Normal file
View File

@ -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
}

View File

@ -2,6 +2,7 @@ package mem
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -30,6 +31,37 @@ type File struct {
pkging pkging.Pkger 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) { func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
if sk, ok := f.reader.(io.Seeker); ok { if sk, ok := f.reader.(io.Seeker); ok {
return sk.Seek(ofpkginget, whence) return sk.Seek(ofpkginget, whence)
@ -205,17 +237,6 @@ func (f *File) Open(name string) (http.File, error) {
return di, nil 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 { // func (f *File) UnmarshalJSON(b []byte) error {
// m := map[string]json.RawMessage{} // m := map[string]json.RawMessage{}
// if err := json.Unmarshal(b, &m); err != nil { // if err := json.Unmarshal(b, &m); err != nil {

View File

@ -1,7 +1,10 @@
package mem package mem
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -41,6 +44,58 @@ type Pkger struct {
current here.Info 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) { func (f *Pkger) Abs(p string) (string, error) {
pt, err := f.Parse(p) pt, err := f.Parse(p)
if err != nil { if err != nil {
@ -106,6 +161,33 @@ func (fx *Pkger) RemoveAll(name string) error {
return nil 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) { func (fx *Pkger) Create(name string) (pkging.File, error) {
fx.MkdirAll("/", 0755) fx.MkdirAll("/", 0755)
pt, err := fx.Parse(name) pt, err := fx.Parse(name)

View File

@ -9,7 +9,7 @@ import (
type Pkger interface { type Pkger interface {
Parse(p string) (Path, error) Parse(p string) (Path, error)
Abs(string) (string, error) Abs(p string) (string, error)
AbsPath(Path) (string, error) AbsPath(Path) (string, error)
Current() (here.Info, error) Current() (here.Info, error)

159
pkging/wrap.go Normal file
View File

@ -0,0 +1,159 @@
package pkging
import (
"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) 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
}

54
stuffing/stuffing.go Normal file
View File

@ -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
}