2019-09-03 18:29:28 +03:00
|
|
|
package mem
|
2019-09-01 00:00:24 +03:00
|
|
|
|
|
|
|
import (
|
2019-09-21 19:51:29 +03:00
|
|
|
"encoding/json"
|
2019-09-01 05:45:22 +03:00
|
|
|
"fmt"
|
2019-09-21 19:51:29 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-09-01 05:45:22 +03:00
|
|
|
"os"
|
2019-09-03 22:18:31 +03:00
|
|
|
"path/filepath"
|
2019-09-01 05:45:22 +03:00
|
|
|
"strings"
|
2019-09-03 22:18:31 +03:00
|
|
|
"time"
|
2019-09-01 00:00:24 +03:00
|
|
|
|
|
|
|
"github.com/markbates/pkger/here"
|
|
|
|
"github.com/markbates/pkger/internal/maps"
|
2019-09-02 01:02:45 +03:00
|
|
|
"github.com/markbates/pkger/pkging"
|
2019-09-01 00:00:24 +03:00
|
|
|
)
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
var _ pkging.Pkger = &Pkger{}
|
2019-09-01 00:00:24 +03:00
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func WithInfo(fx *Pkger, infos ...here.Info) {
|
2019-09-01 06:29:25 +03:00
|
|
|
for _, info := range infos {
|
|
|
|
fx.infos.Store(info.ImportPath, info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func New(info here.Info) (*Pkger, error) {
|
|
|
|
f := &Pkger{
|
2019-10-09 20:21:54 +03:00
|
|
|
infos: &maps.Infos{},
|
2019-09-01 05:47:23 +03:00
|
|
|
files: &maps.Files{},
|
|
|
|
current: info,
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|
2019-09-20 17:56:26 +03:00
|
|
|
f.infos.Store(info.ImportPath, info)
|
|
|
|
f.MkdirAll("/", 0755)
|
2019-09-01 00:00:24 +03:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
type Pkger struct {
|
2019-09-01 00:00:24 +03:00
|
|
|
infos *maps.Infos
|
|
|
|
files *maps.Files
|
|
|
|
current here.Info
|
|
|
|
}
|
|
|
|
|
2019-09-21 19:51:29 +03:00
|
|
|
type jay struct {
|
|
|
|
Infos *maps.Infos `json:"infos"`
|
|
|
|
Files map[string]*File `json:"files"`
|
|
|
|
Current here.Info `json:"current"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pkger) MarshalJSON() ([]byte, error) {
|
|
|
|
files := map[string]*File{}
|
|
|
|
|
2019-10-09 20:21:54 +03:00
|
|
|
p.files.Range(func(key here.Path, file pkging.File) bool {
|
2019-09-21 19:51:29 +03:00
|
|
|
f, ok := file.(*File)
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
files[key.String()] = f
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return json.Marshal(jay{
|
|
|
|
Infos: p.infos,
|
|
|
|
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.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
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func (f *Pkger) Abs(p string) (string, error) {
|
2019-09-01 23:18:39 +03:00
|
|
|
pt, err := f.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return f.AbsPath(pt)
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:21:54 +03:00
|
|
|
func (f *Pkger) AbsPath(pt here.Path) (string, error) {
|
2019-09-01 23:18:39 +03:00
|
|
|
return pt.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func (f *Pkger) Current() (here.Info, error) {
|
2019-09-01 00:00:24 +03:00
|
|
|
return f.current, nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func (f *Pkger) Info(p string) (here.Info, error) {
|
2019-09-01 00:00:24 +03:00
|
|
|
info, ok := f.infos.Load(p)
|
2019-09-01 06:29:25 +03:00
|
|
|
if !ok {
|
|
|
|
return info, fmt.Errorf("no such package %q", p)
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:21:54 +03:00
|
|
|
func (f *Pkger) Parse(p string) (here.Path, error) {
|
|
|
|
return f.current.Parse(p)
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func (fx *Pkger) Remove(name string) error {
|
2019-09-01 05:45:22 +03:00
|
|
|
pt, err := fx.Parse(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := fx.files.Load(pt); !ok {
|
|
|
|
return &os.PathError{"remove", pt.String(), fmt.Errorf("no such file or directory")}
|
|
|
|
}
|
|
|
|
|
|
|
|
fx.files.Delete(pt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 18:29:28 +03:00
|
|
|
func (fx *Pkger) RemoveAll(name string) error {
|
2019-09-01 05:45:22 +03:00
|
|
|
pt, err := fx.Parse(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:21:54 +03:00
|
|
|
fx.files.Range(func(key here.Path, file pkging.File) bool {
|
2019-09-03 19:41:21 +03:00
|
|
|
if strings.HasPrefix(key.Name, pt.Name) {
|
|
|
|
fx.files.Delete(key)
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
2019-09-03 19:41:21 +03:00
|
|
|
return true
|
2019-09-01 05:45:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-03 22:18:31 +03:00
|
|
|
|
2019-09-21 19:51:29 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-03 22:18:31 +03:00
|
|
|
func (fx *Pkger) Create(name string) (pkging.File, error) {
|
2019-09-20 17:56:26 +03:00
|
|
|
fx.MkdirAll("/", 0755)
|
2019-09-03 22:18:31 +03:00
|
|
|
pt, err := fx.Parse(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
her, err := fx.Info(pt.Pkg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:56:26 +03:00
|
|
|
dir := filepath.Dir(pt.Name)
|
|
|
|
if _, err := fx.Stat(dir); err != nil {
|
2019-09-03 22:18:31 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f := &File{
|
|
|
|
path: pt,
|
|
|
|
her: her,
|
|
|
|
info: &pkging.FileInfo{
|
|
|
|
Details: pkging.Details{
|
|
|
|
Name: pt.Name,
|
|
|
|
Mode: 0644,
|
|
|
|
ModTime: pkging.ModTime(time.Now()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pkging: fx,
|
|
|
|
}
|
|
|
|
|
|
|
|
fx.files.Store(pt, f)
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error {
|
|
|
|
path, err := fx.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
root := path.Name
|
|
|
|
|
|
|
|
cur, err := fx.Current()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for root != "" {
|
2019-10-09 20:21:54 +03:00
|
|
|
pt := here.Path{
|
2019-09-03 22:18:31 +03:00
|
|
|
Pkg: path.Pkg,
|
|
|
|
Name: root,
|
|
|
|
}
|
|
|
|
if _, ok := fx.files.Load(pt); ok {
|
|
|
|
root = filepath.Dir(root)
|
|
|
|
if root == "/" || root == "\\" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f := &File{
|
|
|
|
pkging: fx,
|
|
|
|
path: pt,
|
|
|
|
her: cur,
|
|
|
|
info: &pkging.FileInfo{
|
|
|
|
Details: pkging.Details{
|
|
|
|
Name: pt.Name,
|
|
|
|
Mode: perm,
|
|
|
|
ModTime: pkging.ModTime(time.Now()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.info.Details.IsDir = true
|
|
|
|
f.info.Details.Mode = perm
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fx.files.Store(pt, f)
|
|
|
|
root = filepath.Dir(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fx *Pkger) Open(name string) (pkging.File, error) {
|
|
|
|
pt, err := fx.Parse(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fl, ok := fx.files.Load(pt)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("could not open %s", name)
|
|
|
|
}
|
|
|
|
f, ok := fl.(*File)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("could not open %s", name)
|
|
|
|
}
|
|
|
|
nf := &File{
|
|
|
|
pkging: fx,
|
|
|
|
info: pkging.WithName(f.info.Name(), f.info),
|
|
|
|
path: f.path,
|
|
|
|
data: f.data,
|
|
|
|
her: f.her,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
|
|
|
|
pt, err := fx.Parse(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, ok := fx.files.Load(pt)
|
|
|
|
if ok {
|
|
|
|
return f.Stat()
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("could not stat %s", pt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
|
|
|
|
keys := f.files.Keys()
|
|
|
|
|
|
|
|
pt, err := f.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
skip := "!"
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
if !strings.HasPrefix(k.Name, pt.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k.Name, skip) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fl, ok := f.files.Load(k)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("could not find %s", k)
|
|
|
|
}
|
|
|
|
fi, err := fl.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi)
|
|
|
|
err = wf(k.String(), fi, nil)
|
|
|
|
if err == filepath.SkipDir {
|
|
|
|
|
|
|
|
skip = k.Name
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|