2019-09-03 18:29:28 +03:00
|
|
|
package mem
|
2019-09-01 00:00:24 +03:00
|
|
|
|
|
|
|
import (
|
2019-09-01 05:45:22 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
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-09-01 06:29:25 +03:00
|
|
|
infos: &maps.Infos{},
|
|
|
|
paths: &maps.Paths{
|
|
|
|
Current: info,
|
|
|
|
},
|
2019-09-01 05:47:23 +03:00
|
|
|
files: &maps.Files{},
|
|
|
|
current: info,
|
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
|
|
|
|
paths *maps.Paths
|
|
|
|
files *maps.Files
|
|
|
|
current here.Info
|
|
|
|
}
|
|
|
|
|
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-09-03 18:29:28 +03:00
|
|
|
func (f *Pkger) AbsPath(pt pkging.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-09-03 18:29:28 +03:00
|
|
|
func (f *Pkger) Parse(p string) (pkging.Path, error) {
|
2019-09-01 00:00:24 +03:00
|
|
|
return f.paths.Parse(p)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return fx.Walk("/", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(path, pt.String()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ph, err := fx.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fx.files.Delete(ph)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|