guarded by monkeys

This commit is contained in:
Mark Bates 2019-09-01 16:18:39 -04:00
parent 5e24d43bb0
commit 6463dbba07
7 changed files with 69 additions and 44 deletions

View File

@ -9,7 +9,7 @@ import (
type File interface { type File interface {
Close() error Close() error
FilePath() string Abs() (string, error)
Info() here.Info Info() here.Info
Name() string Name() string
Open(name string) (http.File, error) Open(name string) (http.File, error)

View File

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

View File

@ -196,7 +196,6 @@ func (s *FileSystem) Test_Stat(t *testing.T) {
pt, err := s.Parse(tt.in) pt, err := s.Parse(tt.in)
r.NoError(err) r.NoError(err)
r.Fail(pt.String())
f, err := s.Create(tt.in) f, err := s.Create(tt.in)
r.NoError(err) r.NoError(err)
_, err = io.Copy(f, strings.NewReader("!"+pt.String())) _, err = io.Copy(f, strings.NewReader("!"+pt.String()))

View File

@ -3,7 +3,6 @@ package hdfs
import ( import (
"net/http" "net/http"
"os" "os"
"strings"
"github.com/markbates/pkger/fs" "github.com/markbates/pkger/fs"
"github.com/markbates/pkger/here" "github.com/markbates/pkger/here"
@ -13,22 +12,18 @@ var _ fs.File = &File{}
type File struct { type File struct {
*os.File *os.File
filePath string info *fs.FileInfo
info *fs.FileInfo her here.Info
her here.Info path fs.Path
path fs.Path fs fs.FileSystem
fs fs.FileSystem
} }
func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) { func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) {
cur, err := fx.Current() pt, err := fx.Parse(osf.Name())
if err != nil { if err != nil {
return nil, err return nil, err
} }
pt := fs.Path{
Name: strings.TrimPrefix(osf.Name(), cur.Dir),
}
info, err := osf.Stat() info, err := osf.Stat()
if err != nil { if err != nil {
@ -36,10 +31,9 @@ func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) {
} }
f := &File{ f := &File{
File: osf, File: osf,
filePath: info.Name(), path: pt,
path: pt, fs: fx,
fs: fx,
} }
f.info = fs.WithName(pt.Name, info) f.info = fs.WithName(pt.Name, info)
@ -55,8 +49,8 @@ func (f *File) Close() error {
return f.File.Close() return f.File.Close()
} }
func (f *File) FilePath() string { func (f *File) Abs() (string, error) {
return f.filePath return f.fs.AbsPath(f.path)
} }
func (f *File) Info() here.Info { func (f *File) Info() here.Info {
@ -76,12 +70,19 @@ func (f *File) Path() fs.Path {
} }
func (f *File) Stat() (os.FileInfo, error) { func (f *File) Stat() (os.FileInfo, error) {
if f.info == nil { if f.info != nil {
info, err := os.Stat(f.filePath) return f.info, nil
if err != nil {
return nil, err
}
f.info = fs.NewFileInfo(info)
} }
return f.info, nil
abs, err := f.Abs()
if err != nil {
return nil, err
}
info, err := os.Stat(abs)
if err != nil {
return nil, err
}
f.info = fs.NewFileInfo(info)
return info, nil
} }

View File

@ -20,6 +20,25 @@ type FS struct {
current here.Info current here.Info
} }
func (f *FS) Abs(p string) (string, error) {
pt, err := f.Parse(p)
if err != nil {
return "", err
}
return f.AbsPath(pt)
}
func (f *FS) AbsPath(pt fs.Path) (string, error) {
if pt.Pkg == f.current.ImportPath {
return filepath.Join(f.current.Dir, pt.Name), nil
}
info, err := f.Info(pt.Pkg)
if err != nil {
return "", err
}
return filepath.Join(info.Dir, pt.Name), nil
}
func New() (*FS, error) { func New() (*FS, error) {
info, err := here.Current() info, err := here.Current()
if err != nil { if err != nil {
@ -35,7 +54,7 @@ func New() (*FS, error) {
} }
func (fx *FS) Create(name string) (fs.File, error) { func (fx *FS) Create(name string) (fs.File, error) {
name, err := fx.locate(name) name, err := fx.Abs(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -68,7 +87,7 @@ func (f *FS) Info(p string) (here.Info, error) {
} }
func (f *FS) MkdirAll(p string, perm os.FileMode) error { func (f *FS) MkdirAll(p string, perm os.FileMode) error {
p, err := f.locate(p) p, err := f.Abs(p)
if err != nil { if err != nil {
return err return err
} }
@ -76,7 +95,7 @@ func (f *FS) MkdirAll(p string, perm os.FileMode) error {
} }
func (fx *FS) Open(name string) (fs.File, error) { func (fx *FS) Open(name string) (fs.File, error) {
name, err := fx.locate(name) name, err := fx.Abs(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -92,7 +111,7 @@ func (f *FS) Parse(p string) (fs.Path, error) {
} }
func (f *FS) ReadFile(s string) ([]byte, error) { func (f *FS) ReadFile(s string) ([]byte, error) {
s, err := f.locate(s) s, err := f.Abs(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -100,7 +119,7 @@ func (f *FS) ReadFile(s string) ([]byte, error) {
} }
func (f *FS) Stat(name string) (os.FileInfo, error) { func (f *FS) Stat(name string) (os.FileInfo, error) {
name, err := f.locate(name) name, err := f.Abs(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -108,7 +127,7 @@ func (f *FS) Stat(name string) (os.FileInfo, error) {
} }
func (f *FS) Walk(p string, wf filepath.WalkFunc) error { func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
fp, err := f.locate(p) fp, err := f.Abs(p)
if err != nil { if err != nil {
return err return err
} }
@ -133,17 +152,8 @@ func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
return err return err
} }
func (f *FS) locate(p string) (string, error) {
pt, err := f.Parse(p)
if err != nil {
return p, err
}
p = f.current.FilePath(pt.Name)
return p, nil
}
func (fx *FS) Remove(name string) error { func (fx *FS) Remove(name string) error {
name, err := fx.locate(name) name, err := fx.Abs(name)
if err != nil { if err != nil {
return err return err
} }
@ -151,7 +161,7 @@ func (fx *FS) Remove(name string) error {
} }
func (fx *FS) RemoveAll(name string) error { func (fx *FS) RemoveAll(name string) error {
name, err := fx.locate(name) name, err := fx.Abs(name)
if err != nil { if err != nil {
return err return err
} }

View File

@ -98,8 +98,8 @@ func (f File) Name() string {
return f.info.Name() return f.info.Name()
} }
func (f File) FilePath() string { func (f File) Abs() (string, error) {
return f.her.FilePath(f.Name()) return f.fs.AbsPath(f.Path())
} }
func (f File) Path() fs.Path { func (f File) Path() fs.Path {

View File

@ -38,6 +38,18 @@ type FS struct {
current here.Info current here.Info
} }
func (f *FS) Abs(p string) (string, error) {
pt, err := f.Parse(p)
if err != nil {
return "", err
}
return f.AbsPath(pt)
}
func (f *FS) AbsPath(pt fs.Path) (string, error) {
return pt.String(), nil
}
func (f *FS) Current() (here.Info, error) { func (f *FS) Current() (here.Info, error) {
return f.current, nil return f.current, nil
} }