pkger/pkging/mem/mem.go

253 lines
5.5 KiB
Go
Raw Permalink Normal View History

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"
2019-10-24 23:22:15 +03:00
"path"
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-10-16 18:07:57 +03:00
// New returns *Pkger for the provided here.Info
2019-09-03 18:29:28 +03:00
func New(info here.Info) (*Pkger, error) {
f := &Pkger{
2019-10-16 23:09:17 +03:00
infos: &maps.Infos{},
files: &maps.Files{},
Here: info,
2019-09-01 00:00:24 +03:00
}
2019-09-20 17:56:26 +03:00
f.infos.Store(info.ImportPath, info)
2019-09-01 00:00:24 +03:00
return f, nil
}
2019-09-03 18:29:28 +03:00
type Pkger struct {
2019-10-16 23:09:17 +03:00
Here here.Info
infos *maps.Infos
files *maps.Files
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Current returns the here.Info representing the current Pkger implementation.
2019-09-03 18:29:28 +03:00
func (f *Pkger) Current() (here.Info, error) {
2019-10-16 23:09:17 +03:00
return f.Here, nil
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Info returns the here.Info of the here.Path
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-16 18:07:57 +03:00
// Parse the string in here.Path format.
2019-10-09 20:21:54 +03:00
func (f *Pkger) Parse(p string) (here.Path, error) {
2019-10-16 23:09:17 +03:00
return f.Here.Parse(p)
2019-09-01 00:00:24 +03:00
}
2019-10-16 18:07:57 +03:00
// Remove removes the named file or (empty) directory.
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-10-16 18:07:57 +03:00
// 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).
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-10-16 18:07:57 +03:00
// 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.
2019-09-03 22:18:31 +03:00
func (fx *Pkger) Create(name string) (pkging.File, error) {
2019-10-18 23:19:45 +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)
2019-10-18 22:16:53 +03:00
if dir != "/" {
if _, err := fx.Stat(dir); err != nil {
return nil, err
}
2019-09-03 22:18:31 +03:00
}
f := &File{
2019-10-21 00:52:16 +03:00
Here: her,
2019-09-03 22:18:31 +03:00
path: pt,
info: &pkging.FileInfo{
Details: pkging.Details{
2019-10-24 23:22:15 +03:00
Name: filepath.Base(name),
2019-09-03 22:18:31 +03:00
Mode: 0644,
ModTime: pkging.ModTime(time.Now()),
},
},
pkging: fx,
}
fx.files.Store(pt, f)
return f, nil
}
2019-10-16 18:07:57 +03:00
// 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.
2019-09-03 22:18:31 +03:00
func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error {
2019-10-24 23:22:15 +03:00
pt, err := fx.Parse(p)
2019-09-03 22:18:31 +03:00
if err != nil {
return err
}
2019-10-24 23:22:15 +03:00
dir, name := path.Split(pt.Name)
2019-09-03 22:18:31 +03:00
2019-10-24 23:22:15 +03:00
if dir != "/" {
if err := fx.MkdirAll(dir, perm); err != nil {
2019-09-03 22:18:31 +03:00
return err
}
}
2019-10-24 23:22:15 +03:00
if dir == "/" && name == "" {
dir = filepath.Base(fx.Here.Dir)
}
f := &File{
Here: fx.Here,
pkging: fx,
path: pt,
info: &pkging.FileInfo{
Details: pkging.Details{
IsDir: true,
Name: name,
Mode: perm,
ModTime: pkging.ModTime(time.Now()),
},
},
}
if err := f.Close(); err != nil {
return err
}
fx.files.Store(pt, f)
2019-09-03 22:18:31 +03:00
return nil
}
2019-10-16 18:07:57 +03:00
// 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.
2019-09-03 22:18:31 +03:00
func (fx *Pkger) Open(name string) (pkging.File, error) {
pt, err := fx.Parse(name)
if err != nil {
2019-10-16 00:37:51 +03:00
return nil, &os.PathError{
Op: "open",
Path: name,
Err: err,
}
2019-09-03 22:18:31 +03:00
}
fl, ok := fx.files.Load(pt)
if !ok {
2019-10-16 00:37:51 +03:00
return nil, os.ErrNotExist
2019-09-03 22:18:31 +03:00
}
f, ok := fl.(*File)
if !ok {
2019-10-16 00:37:51 +03:00
return nil, os.ErrNotExist
2019-09-03 22:18:31 +03:00
}
nf := &File{
pkging: fx,
2019-10-24 23:22:15 +03:00
info: pkging.NewFileInfo(f.info),
2019-09-03 22:18:31 +03:00
path: f.path,
data: f.data,
2019-10-21 00:52:16 +03:00
Here: f.Here,
2019-09-03 22:18:31 +03:00
}
return nf, nil
}
2019-10-16 18:07:57 +03:00
// Stat returns a FileInfo describing the named file.
2019-09-03 22:18:31 +03:00
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)
}
2019-10-16 18:07:57 +03:00
// 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.
2019-09-03 22:18:31 +03:00
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 {
2019-10-18 22:16:53 +03:00
if k.Pkg != pt.Pkg {
continue
}
2019-09-03 22:18:31 +03:00
if !strings.HasPrefix(k.Name, pt.Name) {
continue
}
if strings.HasPrefix(k.Name, skip) {
continue
}
2019-10-18 22:16:53 +03:00
2019-09-03 22:18:31 +03:00
fl, ok := f.files.Load(k)
if !ok {
2019-10-18 22:16:53 +03:00
return os.ErrNotExist
2019-09-03 22:18:31 +03:00
}
2019-10-18 22:16:53 +03:00
2019-09-03 22:18:31 +03:00
fi, err := fl.Stat()
if err != nil {
return err
}
2019-10-24 23:22:15 +03:00
fi = pkging.NewFileInfo(fi)
2019-10-18 22:16:53 +03:00
2019-09-03 22:18:31 +03:00
err = wf(k.String(), fi, nil)
if err == filepath.SkipDir {
skip = k.Name
continue
}
if err != nil {
return err
}
}
return nil
}