Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
guarded by monkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 1, 2019
1 parent 5e24d43 commit 6463dbb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion fs/file.go
Expand Up @@ -9,7 +9,7 @@ import (

type File interface {
Close() error
FilePath() string
Abs() (string, error)
Info() here.Info
Name() string
Open(name string) (http.File, error)
Expand Down
3 changes: 3 additions & 0 deletions fs/fs.go
Expand Up @@ -9,6 +9,9 @@ import (

type FileSystem interface {
Parse(p string) (Path, error)
Abs(string) (string, error)
AbsPath(Path) (string, error)

Current() (here.Info, error)
Info(p string) (here.Info, error)

Expand Down
1 change: 0 additions & 1 deletion fs/fstest/suite.go
Expand Up @@ -196,7 +196,6 @@ func (s *FileSystem) Test_Stat(t *testing.T) {
pt, err := s.Parse(tt.in)
r.NoError(err)

r.Fail(pt.String())
f, err := s.Create(tt.in)
r.NoError(err)
_, err = io.Copy(f, strings.NewReader("!"+pt.String()))
Expand Down
47 changes: 24 additions & 23 deletions fs/hdfs/file.go
Expand Up @@ -3,7 +3,6 @@ package hdfs
import (
"net/http"
"os"
"strings"

"github.com/markbates/pkger/fs"
"github.com/markbates/pkger/here"
Expand All @@ -13,33 +12,28 @@ var _ fs.File = &File{}

type File struct {
*os.File
filePath string
info *fs.FileInfo
her here.Info
path fs.Path
fs fs.FileSystem
info *fs.FileInfo
her here.Info
path fs.Path
fs fs.FileSystem
}

func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) {

cur, err := fx.Current()
pt, err := fx.Parse(osf.Name())
if err != nil {
return nil, err
}
pt := fs.Path{
Name: strings.TrimPrefix(osf.Name(), cur.Dir),
}

info, err := osf.Stat()
if err != nil {
return nil, err
}

f := &File{
File: osf,
filePath: info.Name(),
path: pt,
fs: fx,
File: osf,
path: pt,
fs: fx,
}
f.info = fs.WithName(pt.Name, info)

Expand All @@ -55,8 +49,8 @@ func (f *File) Close() error {
return f.File.Close()
}

func (f *File) FilePath() string {
return f.filePath
func (f *File) Abs() (string, error) {
return f.fs.AbsPath(f.path)
}

func (f *File) Info() here.Info {
Expand All @@ -76,12 +70,19 @@ func (f *File) Path() fs.Path {
}

func (f *File) Stat() (os.FileInfo, error) {
if f.info == nil {
info, err := os.Stat(f.filePath)
if err != nil {
return nil, err
}
f.info = fs.NewFileInfo(info)
if f.info != nil {
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
}
return f.info, nil
f.info = fs.NewFileInfo(info)
return info, nil
}
44 changes: 27 additions & 17 deletions fs/hdfs/hdfs.go
Expand Up @@ -20,6 +20,25 @@ type FS struct {
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) {
info, err := here.Current()
if err != nil {
Expand All @@ -35,7 +54,7 @@ func New() (*FS, error) {
}

func (fx *FS) Create(name string) (fs.File, error) {
name, err := fx.locate(name)
name, err := fx.Abs(name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -68,15 +87,15 @@ func (f *FS) Info(p string) (here.Info, error) {
}

func (f *FS) MkdirAll(p string, perm os.FileMode) error {
p, err := f.locate(p)
p, err := f.Abs(p)
if err != nil {
return err
}
return os.MkdirAll(p, perm)
}

func (fx *FS) Open(name string) (fs.File, error) {
name, err := fx.locate(name)
name, err := fx.Abs(name)
if err != nil {
return nil, err
}
Expand All @@ -92,23 +111,23 @@ func (f *FS) Parse(p string) (fs.Path, error) {
}

func (f *FS) ReadFile(s string) ([]byte, error) {
s, err := f.locate(s)
s, err := f.Abs(s)
if err != nil {
return nil, err
}
return ioutil.ReadFile(s)
}

func (f *FS) Stat(name string) (os.FileInfo, error) {
name, err := f.locate(name)
name, err := f.Abs(name)
if err != nil {
return nil, err
}
return os.Stat(name)
}

func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
fp, err := f.locate(p)
fp, err := f.Abs(p)
if err != nil {
return err
}
Expand All @@ -133,25 +152,16 @@ func (f *FS) Walk(p string, wf filepath.WalkFunc) error {
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 {
name, err := fx.locate(name)
name, err := fx.Abs(name)
if err != nil {
return err
}
return os.Remove(name)
}

func (fx *FS) RemoveAll(name string) error {
name, err := fx.locate(name)
name, err := fx.Abs(name)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions fs/memfs/file.go
Expand Up @@ -98,8 +98,8 @@ func (f File) Name() string {
return f.info.Name()
}

func (f File) FilePath() string {
return f.her.FilePath(f.Name())
func (f File) Abs() (string, error) {
return f.fs.AbsPath(f.Path())
}

func (f File) Path() fs.Path {
Expand Down
12 changes: 12 additions & 0 deletions fs/memfs/memfs.go
Expand Up @@ -38,6 +38,18 @@ type FS struct {
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) {
return f.current, nil
}
Expand Down

0 comments on commit 6463dbb

Please sign in to comment.