forked from mirror/pkger
but we unleashed a lion
This commit is contained in:
parent
4e3e96b0ec
commit
d3b670ed37
|
@ -12,15 +12,15 @@ import (
|
|||
"github.com/markbates/pkger/internal/maps"
|
||||
)
|
||||
|
||||
var _ fs.Warehouse = &FS{}
|
||||
var _ fs.Warehouse = &Warehouse{}
|
||||
|
||||
type FS struct {
|
||||
type Warehouse struct {
|
||||
infos *maps.Infos
|
||||
paths *maps.Paths
|
||||
current here.Info
|
||||
}
|
||||
|
||||
func (f *FS) Abs(p string) (string, error) {
|
||||
func (f *Warehouse) Abs(p string) (string, error) {
|
||||
pt, err := f.Parse(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -28,7 +28,7 @@ func (f *FS) Abs(p string) (string, error) {
|
|||
return f.AbsPath(pt)
|
||||
}
|
||||
|
||||
func (f *FS) AbsPath(pt fs.Path) (string, error) {
|
||||
func (f *Warehouse) AbsPath(pt fs.Path) (string, error) {
|
||||
if pt.Pkg == f.current.ImportPath {
|
||||
return filepath.Join(f.current.Dir, pt.Name), nil
|
||||
}
|
||||
|
@ -39,12 +39,12 @@ func (f *FS) AbsPath(pt fs.Path) (string, error) {
|
|||
return filepath.Join(info.Dir, pt.Name), nil
|
||||
}
|
||||
|
||||
func New() (*FS, error) {
|
||||
func New() (*Warehouse, error) {
|
||||
info, err := here.Current()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FS{
|
||||
return &Warehouse{
|
||||
infos: &maps.Infos{},
|
||||
paths: &maps.Paths{
|
||||
Current: info,
|
||||
|
@ -53,7 +53,7 @@ func New() (*FS, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (fx *FS) Create(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Create(name string) (fs.File, error) {
|
||||
name, err := fx.Abs(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -68,11 +68,11 @@ func (fx *FS) Create(name string) (fs.File, error) {
|
|||
return NewFile(fx, f)
|
||||
}
|
||||
|
||||
func (f *FS) Current() (here.Info, error) {
|
||||
func (f *Warehouse) Current() (here.Info, error) {
|
||||
return f.current, nil
|
||||
}
|
||||
|
||||
func (f *FS) Info(p string) (here.Info, error) {
|
||||
func (f *Warehouse) Info(p string) (here.Info, error) {
|
||||
info, ok := f.infos.Load(p)
|
||||
if ok {
|
||||
return info, nil
|
||||
|
@ -86,7 +86,7 @@ func (f *FS) Info(p string) (here.Info, error) {
|
|||
return info, nil
|
||||
}
|
||||
|
||||
func (f *FS) MkdirAll(p string, perm os.FileMode) error {
|
||||
func (f *Warehouse) MkdirAll(p string, perm os.FileMode) error {
|
||||
p, err := f.Abs(p)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -94,7 +94,7 @@ func (f *FS) MkdirAll(p string, perm os.FileMode) error {
|
|||
return os.MkdirAll(p, perm)
|
||||
}
|
||||
|
||||
func (fx *FS) Open(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Open(name string) (fs.File, error) {
|
||||
name, err := fx.Abs(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -106,11 +106,11 @@ func (fx *FS) Open(name string) (fs.File, error) {
|
|||
return NewFile(fx, f)
|
||||
}
|
||||
|
||||
func (f *FS) Parse(p string) (fs.Path, error) {
|
||||
func (f *Warehouse) Parse(p string) (fs.Path, error) {
|
||||
return f.paths.Parse(p)
|
||||
}
|
||||
|
||||
func (f *FS) ReadFile(s string) ([]byte, error) {
|
||||
func (f *Warehouse) ReadFile(s string) ([]byte, error) {
|
||||
s, err := f.Abs(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -118,7 +118,7 @@ func (f *FS) ReadFile(s string) ([]byte, error) {
|
|||
return ioutil.ReadFile(s)
|
||||
}
|
||||
|
||||
func (f *FS) Stat(name string) (os.FileInfo, error) {
|
||||
func (f *Warehouse) Stat(name string) (os.FileInfo, error) {
|
||||
pt, err := f.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -139,7 +139,7 @@ func (f *FS) Stat(name string) (os.FileInfo, error) {
|
|||
return info, nil
|
||||
}
|
||||
|
||||
func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
|
||||
func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error {
|
||||
fp, err := f.Abs(p)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -165,7 +165,7 @@ func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (fx *FS) Remove(name string) error {
|
||||
func (fx *Warehouse) Remove(name string) error {
|
||||
name, err := fx.Abs(name)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -173,7 +173,7 @@ func (fx *FS) Remove(name string) error {
|
|||
return os.Remove(name)
|
||||
}
|
||||
|
||||
func (fx *FS) RemoveAll(name string) error {
|
||||
func (fx *Warehouse) RemoveAll(name string) error {
|
||||
name, err := fx.Abs(name)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_FS(t *testing.T) {
|
||||
func Test_Warehouse(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
myfs, err := New()
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/markbates/pkger/fs"
|
||||
)
|
||||
|
||||
func (fx *FS) Create(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Create(name string) (fs.File, error) {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -3,7 +3,7 @@ package memwh
|
|||
// func Test_HTTP_Dir(t *testing.T) {
|
||||
// r := require.New(t)
|
||||
//
|
||||
// fs := NewFS()
|
||||
// fs := NewWarehouse()
|
||||
//
|
||||
// r.NoError(Folder.Create(fs))
|
||||
//
|
||||
|
@ -24,7 +24,7 @@ package memwh
|
|||
// func Test_HTTP_File_Memory(t *testing.T) {
|
||||
// r := require.New(t)
|
||||
//
|
||||
// fs := NewFS()
|
||||
// fs := NewWarehouse()
|
||||
// r.NoError(Folder.Create(fs))
|
||||
//
|
||||
// dir, err := fs.Open("/")
|
||||
|
@ -44,7 +44,7 @@ package memwh
|
|||
// func Test_HTTP_Dir_Memory_StripPrefix(t *testing.T) {
|
||||
// r := require.New(t)
|
||||
//
|
||||
// fs := NewFS()
|
||||
// fs := NewWarehouse()
|
||||
// r.NoError(Folder.Create(fs))
|
||||
//
|
||||
// dir, err := fs.Open("/public")
|
||||
|
|
|
@ -11,16 +11,16 @@ import (
|
|||
"github.com/markbates/pkger/internal/maps"
|
||||
)
|
||||
|
||||
var _ fs.Warehouse = &FS{}
|
||||
var _ fs.Warehouse = &Warehouse{}
|
||||
|
||||
func WithInfo(fx *FS, infos ...here.Info) {
|
||||
func WithInfo(fx *Warehouse, infos ...here.Info) {
|
||||
for _, info := range infos {
|
||||
fx.infos.Store(info.ImportPath, info)
|
||||
}
|
||||
}
|
||||
|
||||
func New(info here.Info) (*FS, error) {
|
||||
f := &FS{
|
||||
func New(info here.Info) (*Warehouse, error) {
|
||||
f := &Warehouse{
|
||||
infos: &maps.Infos{},
|
||||
paths: &maps.Paths{
|
||||
Current: info,
|
||||
|
@ -31,14 +31,14 @@ func New(info here.Info) (*FS, error) {
|
|||
return f, nil
|
||||
}
|
||||
|
||||
type FS struct {
|
||||
type Warehouse struct {
|
||||
infos *maps.Infos
|
||||
paths *maps.Paths
|
||||
files *maps.Files
|
||||
current here.Info
|
||||
}
|
||||
|
||||
func (f *FS) Abs(p string) (string, error) {
|
||||
func (f *Warehouse) Abs(p string) (string, error) {
|
||||
pt, err := f.Parse(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -46,15 +46,15 @@ func (f *FS) Abs(p string) (string, error) {
|
|||
return f.AbsPath(pt)
|
||||
}
|
||||
|
||||
func (f *FS) AbsPath(pt fs.Path) (string, error) {
|
||||
func (f *Warehouse) AbsPath(pt fs.Path) (string, error) {
|
||||
return pt.String(), nil
|
||||
}
|
||||
|
||||
func (f *FS) Current() (here.Info, error) {
|
||||
func (f *Warehouse) Current() (here.Info, error) {
|
||||
return f.current, nil
|
||||
}
|
||||
|
||||
func (f *FS) Info(p string) (here.Info, error) {
|
||||
func (f *Warehouse) Info(p string) (here.Info, error) {
|
||||
info, ok := f.infos.Load(p)
|
||||
if !ok {
|
||||
return info, fmt.Errorf("no such package %q", p)
|
||||
|
@ -63,11 +63,11 @@ func (f *FS) Info(p string) (here.Info, error) {
|
|||
return info, nil
|
||||
}
|
||||
|
||||
func (f *FS) Parse(p string) (fs.Path, error) {
|
||||
func (f *Warehouse) Parse(p string) (fs.Path, error) {
|
||||
return f.paths.Parse(p)
|
||||
}
|
||||
|
||||
func (fx *FS) ReadFile(s string) ([]byte, error) {
|
||||
func (fx *Warehouse) ReadFile(s string) ([]byte, error) {
|
||||
f, err := fx.Open(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -76,7 +76,7 @@ func (fx *FS) ReadFile(s string) ([]byte, error) {
|
|||
return ioutil.ReadAll(f)
|
||||
}
|
||||
|
||||
func (fx *FS) Remove(name string) error {
|
||||
func (fx *Warehouse) Remove(name string) error {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -90,7 +90,7 @@ func (fx *FS) Remove(name string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (fx *FS) RemoveAll(name string) error {
|
||||
func (fx *Warehouse) RemoveAll(name string) error {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_FS(t *testing.T) {
|
||||
func Test_Warehouse(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
info, err := here.Current()
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/markbates/pkger/fs"
|
||||
)
|
||||
|
||||
func (fx *FS) MkdirAll(p string, perm os.FileMode) error {
|
||||
func (fx *Warehouse) MkdirAll(p string, perm os.FileMode) error {
|
||||
path, err := fx.Parse(p)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/markbates/pkger/fs"
|
||||
)
|
||||
|
||||
func (fx *FS) Open(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Open(name string) (fs.File, error) {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
func (fx *FS) Stat(name string) (os.FileInfo, error) {
|
||||
func (fx *Warehouse) Stat(name string) (os.FileInfo, error) {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/markbates/pkger/fs"
|
||||
)
|
||||
|
||||
func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
|
||||
func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error {
|
||||
keys := f.files.Keys()
|
||||
|
||||
pt, err := f.Parse(p)
|
||||
|
|
Loading…
Reference in New Issue