mirror of https://github.com/markbates/pkger.git
bottles and flowers
This commit is contained in:
parent
d2fa5f9f73
commit
3d94a20bbf
|
@ -1,12 +0,0 @@
|
|||
package fstest
|
||||
|
||||
import (
|
||||
"github.com/markbates/pkger/fs"
|
||||
)
|
||||
|
||||
type TestFile struct {
|
||||
Name string
|
||||
Path fs.Path
|
||||
}
|
||||
|
||||
type TestFiles map[fs.Path]TestFile
|
|
@ -1,24 +0,0 @@
|
|||
package hdware
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/fs/fstest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Warehouse(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
myfs, err := New()
|
||||
r.NoError(err)
|
||||
|
||||
myfs.current.Dir = filepath.Join(myfs.current.Dir, ".fstest")
|
||||
myfs.paths.Current = myfs.current
|
||||
|
||||
suite, err := fstest.NewSuite(myfs)
|
||||
r.NoError(err)
|
||||
|
||||
suite.Test(t)
|
||||
}
|
|
@ -8,12 +8,12 @@ import (
|
|||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
// Files wraps sync.Map and uses the following types:
|
||||
// key: fs.Path
|
||||
// value: fs.File
|
||||
// key: pkging.Path
|
||||
// value: pkging.File
|
||||
type Files struct {
|
||||
data *sync.Map
|
||||
once *sync.Once
|
||||
|
@ -52,13 +52,13 @@ func (m *Files) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
func (m *Files) UnmarshalJSON(b []byte) error {
|
||||
mm := map[string]fs.File{}
|
||||
mm := map[string]pkging.File{}
|
||||
|
||||
if err := json.Unmarshal(b, &mm); err != nil {
|
||||
return err
|
||||
}
|
||||
for k, v := range mm {
|
||||
var pt fs.Path
|
||||
var pt pkging.Path
|
||||
if err := json.Unmarshal([]byte(k), &pt); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -68,34 +68,34 @@ func (m *Files) UnmarshalJSON(b []byte) error {
|
|||
}
|
||||
|
||||
// Delete the key from the map
|
||||
func (m *Files) Delete(key fs.Path) {
|
||||
func (m *Files) Delete(key pkging.Path) {
|
||||
m.Data().Delete(key)
|
||||
}
|
||||
|
||||
// Load the key from the map.
|
||||
// Returns fs.File or bool.
|
||||
// Returns pkging.File or bool.
|
||||
// A false return indicates either the key was not found
|
||||
// or the value is not of type fs.File
|
||||
func (m *Files) Load(key fs.Path) (fs.File, bool) {
|
||||
// or the value is not of type pkging.File
|
||||
func (m *Files) Load(key pkging.Path) (pkging.File, bool) {
|
||||
i, ok := m.Data().Load(key)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
s, ok := i.(fs.File)
|
||||
s, ok := i.(pkging.File)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// LoadOrStore will return an existing key or
|
||||
// store the value if not already in the map
|
||||
func (m *Files) LoadOrStore(key fs.Path, value fs.File) (fs.File, bool) {
|
||||
func (m *Files) LoadOrStore(key pkging.Path, value pkging.File) (pkging.File, bool) {
|
||||
i, _ := m.Data().LoadOrStore(key, value)
|
||||
s, ok := i.(fs.File)
|
||||
s, ok := i.(pkging.File)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// LoadOr will return an existing key or
|
||||
// run the function and store the results
|
||||
func (m *Files) LoadOr(key fs.Path, fn func(*Files) (fs.File, bool)) (fs.File, bool) {
|
||||
func (m *Files) LoadOr(key pkging.Path, fn func(*Files) (pkging.File, bool)) (pkging.File, bool) {
|
||||
i, ok := m.Load(key)
|
||||
if ok {
|
||||
return i, ok
|
||||
|
@ -108,14 +108,14 @@ func (m *Files) LoadOr(key fs.Path, fn func(*Files) (fs.File, bool)) (fs.File, b
|
|||
return i, false
|
||||
}
|
||||
|
||||
// Range over the fs.File values in the map
|
||||
func (m *Files) Range(f func(key fs.Path, value fs.File) bool) {
|
||||
// Range over the pkging.File values in the map
|
||||
func (m *Files) Range(f func(key pkging.Path, value pkging.File) bool) {
|
||||
m.Data().Range(func(k, v interface{}) bool {
|
||||
key, ok := k.(fs.Path)
|
||||
key, ok := k.(pkging.Path)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
value, ok := v.(fs.File)
|
||||
value, ok := v.(pkging.File)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
@ -123,15 +123,15 @@ func (m *Files) Range(f func(key fs.Path, value fs.File) bool) {
|
|||
})
|
||||
}
|
||||
|
||||
// Store a fs.File in the map
|
||||
func (m *Files) Store(key fs.Path, value fs.File) {
|
||||
// Store a pkging.File in the map
|
||||
func (m *Files) Store(key pkging.Path, value pkging.File) {
|
||||
m.Data().Store(key, value)
|
||||
}
|
||||
|
||||
// Keys returns a list of keys in the map
|
||||
func (m *Files) Keys() []fs.Path {
|
||||
var keys []fs.Path
|
||||
m.Range(func(key fs.Path, value fs.File) bool {
|
||||
func (m *Files) Keys() []pkging.Path {
|
||||
var keys []pkging.Path
|
||||
m.Range(func(key pkging.Path, value pkging.File) bool {
|
||||
keys = append(keys, key)
|
||||
return true
|
||||
})
|
||||
|
|
|
@ -10,8 +10,8 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
// Paths wraps sync.Map and uses the following types:
|
||||
|
@ -45,7 +45,7 @@ func (m *Paths) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
func (m *Paths) UnmarshalJSON(b []byte) error {
|
||||
mm := map[string]fs.Path{}
|
||||
mm := map[string]pkging.Path{}
|
||||
|
||||
if err := json.Unmarshal(b, &mm); err != nil {
|
||||
return err
|
||||
|
@ -65,26 +65,26 @@ func (m *Paths) Delete(key string) {
|
|||
// Returns Path or bool.
|
||||
// A false return indicates either the key was not found
|
||||
// or the value is not of type Path
|
||||
func (m *Paths) Load(key string) (fs.Path, bool) {
|
||||
func (m *Paths) Load(key string) (pkging.Path, bool) {
|
||||
i, ok := m.Data().Load(key)
|
||||
if !ok {
|
||||
return fs.Path{}, false
|
||||
return pkging.Path{}, false
|
||||
}
|
||||
s, ok := i.(fs.Path)
|
||||
s, ok := i.(pkging.Path)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// LoadOrStore will return an existing key or
|
||||
// store the value if not already in the map
|
||||
func (m *Paths) LoadOrStore(key string, value fs.Path) (fs.Path, bool) {
|
||||
func (m *Paths) LoadOrStore(key string, value pkging.Path) (pkging.Path, bool) {
|
||||
i, _ := m.Data().LoadOrStore(key, value)
|
||||
s, ok := i.(fs.Path)
|
||||
s, ok := i.(pkging.Path)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// LoadOr will return an existing key or
|
||||
// run the function and store the results
|
||||
func (m *Paths) LoadOr(key string, fn func(*Paths) (fs.Path, bool)) (fs.Path, bool) {
|
||||
func (m *Paths) LoadOr(key string, fn func(*Paths) (pkging.Path, bool)) (pkging.Path, bool) {
|
||||
i, ok := m.Load(key)
|
||||
if ok {
|
||||
return i, ok
|
||||
|
@ -98,13 +98,13 @@ func (m *Paths) LoadOr(key string, fn func(*Paths) (fs.Path, bool)) (fs.Path, bo
|
|||
}
|
||||
|
||||
// Range over the Path values in the map
|
||||
func (m *Paths) Range(f func(key string, value fs.Path) bool) {
|
||||
func (m *Paths) Range(f func(key string, value pkging.Path) bool) {
|
||||
m.Data().Range(func(k, v interface{}) bool {
|
||||
key, ok := k.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
value, ok := v.(fs.Path)
|
||||
value, ok := v.(pkging.Path)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
@ -113,14 +113,14 @@ func (m *Paths) Range(f func(key string, value fs.Path) bool) {
|
|||
}
|
||||
|
||||
// Store a Path in the map
|
||||
func (m *Paths) Store(key string, value fs.Path) {
|
||||
func (m *Paths) Store(key string, value pkging.Path) {
|
||||
m.Data().Store(key, value)
|
||||
}
|
||||
|
||||
// Keys returns a list of keys in the map
|
||||
func (m *Paths) Keys() []string {
|
||||
var keys []string
|
||||
m.Range(func(key string, value fs.Path) bool {
|
||||
m.Range(func(key string, value pkging.Path) bool {
|
||||
keys = append(keys, key)
|
||||
return true
|
||||
})
|
||||
|
@ -128,7 +128,7 @@ func (m *Paths) Keys() []string {
|
|||
return keys
|
||||
}
|
||||
|
||||
func (m *Paths) Parse(p string) (fs.Path, error) {
|
||||
func (m *Paths) Parse(p string) (pkging.Path, error) {
|
||||
p = strings.Replace(p, "\\", "/", -1)
|
||||
p = strings.TrimSpace(p)
|
||||
|
||||
|
@ -156,8 +156,8 @@ func (m *Paths) Parse(p string) (fs.Path, error) {
|
|||
|
||||
var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?")
|
||||
|
||||
func (m *Paths) build(p, pkg, name string) (fs.Path, error) {
|
||||
pt := fs.Path{
|
||||
func (m *Paths) build(p, pkg, name string) (pkging.Path, error) {
|
||||
pt := pkging.Path{
|
||||
Pkg: pkg,
|
||||
Name: name,
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package fs
|
||||
package pkging
|
||||
|
||||
import (
|
||||
"net/http"
|
|
@ -1,4 +1,4 @@
|
|||
package fs
|
||||
package pkging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -4,10 +4,10 @@ import (
|
|||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func (fx *Warehouse) Create(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Create(name string) (pkging.File, error) {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -20,14 +20,14 @@ func (fx *Warehouse) Create(name string) (fs.File, error) {
|
|||
f := &File{
|
||||
path: pt,
|
||||
her: her,
|
||||
info: &fs.FileInfo{
|
||||
Details: fs.Details{
|
||||
info: &pkging.FileInfo{
|
||||
Details: pkging.Details{
|
||||
Name: pt.Name,
|
||||
Mode: 0644,
|
||||
ModTime: fs.ModTime(time.Now()),
|
||||
ModTime: pkging.ModTime(time.Now()),
|
||||
},
|
||||
},
|
||||
fs: fx,
|
||||
pkging: fx,
|
||||
}
|
||||
|
||||
fx.files.Store(pt, f)
|
|
@ -11,28 +11,28 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
const timeFmt = time.RFC3339Nano
|
||||
|
||||
var _ fs.File = &File{}
|
||||
var _ pkging.File = &File{}
|
||||
|
||||
type File struct {
|
||||
info *fs.FileInfo
|
||||
info *pkging.FileInfo
|
||||
her here.Info
|
||||
path fs.Path
|
||||
path pkging.Path
|
||||
data []byte
|
||||
parent fs.Path
|
||||
parent pkging.Path
|
||||
writer *bytes.Buffer
|
||||
reader io.Reader
|
||||
fs fs.Warehouse
|
||||
pkging pkging.Warehouse
|
||||
}
|
||||
|
||||
func (f *File) Seek(offset int64, whence int) (int64, error) {
|
||||
func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
|
||||
if sk, ok := f.reader.(io.Seeker); ok {
|
||||
return sk.Seek(offset, whence)
|
||||
return sk.Seek(ofpkginget, whence)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ func (f *File) Close() error {
|
|||
|
||||
fi := f.info
|
||||
fi.Details.Size = int64(len(f.data))
|
||||
fi.Details.ModTime = fs.ModTime(time.Now())
|
||||
fi.Details.ModTime = pkging.ModTime(time.Now())
|
||||
f.info = fi
|
||||
return nil
|
||||
}
|
||||
|
@ -99,10 +99,10 @@ func (f File) Name() string {
|
|||
}
|
||||
|
||||
func (f File) Abs() (string, error) {
|
||||
return f.fs.AbsPath(f.Path())
|
||||
return f.pkging.AbsPath(f.Path())
|
||||
}
|
||||
|
||||
func (f File) Path() fs.Path {
|
||||
func (f File) Path() pkging.Path {
|
||||
return f.path
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ func (f File) Format(st fmt.State, verb rune) {
|
|||
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
||||
var infos []os.FileInfo
|
||||
root := f.Path().String()
|
||||
err := f.fs.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
err := f.pkging.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|||
return io.EOF
|
||||
}
|
||||
|
||||
pt, err := f.fs.Parse(path)
|
||||
pt, err := f.pkging.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|||
return nil
|
||||
}
|
||||
// if f.parent.Name != "/" {
|
||||
info = fs.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
|
||||
info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
|
||||
// }
|
||||
infos = append(infos, info)
|
||||
return nil
|
||||
|
@ -169,7 +169,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|||
}
|
||||
|
||||
func (f *File) Open(name string) (http.File, error) {
|
||||
pt, err := f.fs.Parse(name)
|
||||
pt, err := f.pkging.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ func (f *File) Open(name string) (http.File, error) {
|
|||
|
||||
pt.Name = path.Join(f.Path().Name, pt.Name)
|
||||
|
||||
di, err := f.fs.Open(pt.String())
|
||||
di, err := f.pkging.Open(pt.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -191,11 +191,11 @@ func (f *File) Open(name string) (http.File, error) {
|
|||
}
|
||||
if fi.IsDir() {
|
||||
d2 := &File{
|
||||
info: fs.NewFileInfo(fi),
|
||||
info: pkging.NewFileInfo(fi),
|
||||
her: di.Info(),
|
||||
path: pt,
|
||||
parent: f.path,
|
||||
fs: f.fs,
|
||||
pkging: f.pkging,
|
||||
}
|
||||
di = d2
|
||||
}
|
|
@ -4,7 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func (f File) MarshalJSON() ([]byte, error) {
|
||||
|
@ -29,7 +29,7 @@ func (f *File) UnmarshalJSON(b []byte) error {
|
|||
return fmt.Errorf("missing info")
|
||||
}
|
||||
|
||||
f.info = &fs.FileInfo{}
|
||||
f.info = &pkging.FileInfo{}
|
||||
if err := json.Unmarshal(info, f.info); err != nil {
|
||||
return err
|
||||
}
|
|
@ -5,7 +5,7 @@ import (
|
|||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func (fx *Warehouse) MkdirAll(p string, perm os.FileMode) error {
|
||||
|
@ -20,7 +20,7 @@ func (fx *Warehouse) MkdirAll(p string, perm os.FileMode) error {
|
|||
return err
|
||||
}
|
||||
for root != "" {
|
||||
pt := fs.Path{
|
||||
pt := pkging.Path{
|
||||
Pkg: path.Pkg,
|
||||
Name: root,
|
||||
}
|
||||
|
@ -32,14 +32,14 @@ func (fx *Warehouse) MkdirAll(p string, perm os.FileMode) error {
|
|||
continue
|
||||
}
|
||||
f := &File{
|
||||
fs: fx,
|
||||
path: pt,
|
||||
her: cur,
|
||||
info: &fs.FileInfo{
|
||||
Details: fs.Details{
|
||||
pkging: fx,
|
||||
path: pt,
|
||||
her: cur,
|
||||
info: &pkging.FileInfo{
|
||||
Details: pkging.Details{
|
||||
Name: pt.Name,
|
||||
Mode: perm,
|
||||
ModTime: fs.ModTime(time.Now()),
|
||||
ModTime: pkging.ModTime(time.Now()),
|
||||
},
|
||||
},
|
||||
}
|
|
@ -3,10 +3,10 @@ package memware
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func (fx *Warehouse) Open(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Open(name string) (pkging.File, error) {
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -21,11 +21,11 @@ func (fx *Warehouse) Open(name string) (fs.File, error) {
|
|||
return nil, fmt.Errorf("could not open %s", name)
|
||||
}
|
||||
nf := &File{
|
||||
fs: fx,
|
||||
info: fs.WithName(f.info.Name(), f.info),
|
||||
path: f.path,
|
||||
data: f.data,
|
||||
her: f.her,
|
||||
pkging: fx,
|
||||
info: pkging.WithName(f.info.Name(), f.info),
|
||||
path: f.path,
|
||||
data: f.data,
|
||||
her: f.her,
|
||||
}
|
||||
|
||||
return nf, nil
|
|
@ -5,7 +5,7 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error {
|
||||
|
@ -28,7 +28,7 @@ func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error {
|
|||
return err
|
||||
}
|
||||
|
||||
fi = fs.WithName(strings.TrimPrefix(k.Name, pt.Name), fi)
|
||||
fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi)
|
||||
err = wf(k.String(), fi, nil)
|
||||
if err != nil {
|
||||
return err
|
|
@ -6,12 +6,12 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/internal/maps"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
var _ fs.Warehouse = &Warehouse{}
|
||||
var _ pkging.Warehouse = &Warehouse{}
|
||||
|
||||
func WithInfo(fx *Warehouse, infos ...here.Info) {
|
||||
for _, info := range infos {
|
||||
|
@ -46,7 +46,7 @@ func (f *Warehouse) Abs(p string) (string, error) {
|
|||
return f.AbsPath(pt)
|
||||
}
|
||||
|
||||
func (f *Warehouse) AbsPath(pt fs.Path) (string, error) {
|
||||
func (f *Warehouse) AbsPath(pt pkging.Path) (string, error) {
|
||||
return pt.String(), nil
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ func (f *Warehouse) Info(p string) (here.Info, error) {
|
|||
return info, nil
|
||||
}
|
||||
|
||||
func (f *Warehouse) Parse(p string) (fs.Path, error) {
|
||||
func (f *Warehouse) Parse(p string) (pkging.Path, error) {
|
||||
return f.paths.Parse(p)
|
||||
}
|
||||
|
|
@ -3,8 +3,8 @@ package memware
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/fs/fstest"
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging/waretest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
@ -15,12 +15,12 @@ func Test_Warehouse(t *testing.T) {
|
|||
r.NoError(err)
|
||||
r.NotZero(info)
|
||||
|
||||
myfs, err := New(info)
|
||||
mypkging, err := New(info)
|
||||
r.NoError(err)
|
||||
|
||||
WithInfo(myfs, info)
|
||||
WithInfo(mypkging, info)
|
||||
|
||||
suite, err := fstest.NewSuite(myfs)
|
||||
suite, err := waretest.NewSuite(mypkging)
|
||||
r.NoError(err)
|
||||
|
||||
suite.Test(t)
|
|
@ -1,4 +1,4 @@
|
|||
package fs
|
||||
package pkging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -4,21 +4,21 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
var _ fs.File = &File{}
|
||||
var _ pkging.File = &File{}
|
||||
|
||||
type File struct {
|
||||
*os.File
|
||||
info *fs.FileInfo
|
||||
her here.Info
|
||||
path fs.Path
|
||||
fs fs.Warehouse
|
||||
info *pkging.FileInfo
|
||||
her here.Info
|
||||
path pkging.Path
|
||||
pkging pkging.Warehouse
|
||||
}
|
||||
|
||||
func NewFile(fx fs.Warehouse, osf *os.File) (*File, error) {
|
||||
func NewFile(fx pkging.Warehouse, osf *os.File) (*File, error) {
|
||||
|
||||
pt, err := fx.Parse(osf.Name())
|
||||
if err != nil {
|
||||
|
@ -31,11 +31,11 @@ func NewFile(fx fs.Warehouse, osf *os.File) (*File, error) {
|
|||
}
|
||||
|
||||
f := &File{
|
||||
File: osf,
|
||||
path: pt,
|
||||
fs: fx,
|
||||
File: osf,
|
||||
path: pt,
|
||||
pkging: fx,
|
||||
}
|
||||
f.info = fs.WithName(pt.Name, info)
|
||||
f.info = pkging.WithName(pt.Name, info)
|
||||
|
||||
her, err := here.Package(pt.Pkg)
|
||||
if err != nil {
|
||||
|
@ -50,7 +50,7 @@ func (f *File) Close() error {
|
|||
}
|
||||
|
||||
func (f *File) Abs() (string, error) {
|
||||
return f.fs.AbsPath(f.path)
|
||||
return f.pkging.AbsPath(f.path)
|
||||
}
|
||||
|
||||
func (f *File) Info() here.Info {
|
||||
|
@ -65,7 +65,7 @@ func (f *File) Open(name string) (http.File, error) {
|
|||
return f.File, nil
|
||||
}
|
||||
|
||||
func (f *File) Path() fs.Path {
|
||||
func (f *File) Path() pkging.Path {
|
||||
return f.path
|
||||
}
|
||||
|
||||
|
@ -83,6 +83,6 @@ func (f *File) Stat() (os.FileInfo, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.info = fs.NewFileInfo(info)
|
||||
f.info = pkging.NewFileInfo(info)
|
||||
return info, nil
|
||||
}
|
|
@ -7,12 +7,12 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/internal/maps"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
var _ fs.Warehouse = &Warehouse{}
|
||||
var _ pkging.Warehouse = &Warehouse{}
|
||||
|
||||
type Warehouse struct {
|
||||
infos *maps.Infos
|
||||
|
@ -28,7 +28,7 @@ func (f *Warehouse) Abs(p string) (string, error) {
|
|||
return f.AbsPath(pt)
|
||||
}
|
||||
|
||||
func (f *Warehouse) AbsPath(pt fs.Path) (string, error) {
|
||||
func (f *Warehouse) AbsPath(pt pkging.Path) (string, error) {
|
||||
if pt.Pkg == f.current.ImportPath {
|
||||
return filepath.Join(f.current.Dir, pt.Name), nil
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func New() (*Warehouse, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (fx *Warehouse) Create(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Create(name string) (pkging.File, error) {
|
||||
name, err := fx.Abs(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -94,7 +94,7 @@ func (f *Warehouse) MkdirAll(p string, perm os.FileMode) error {
|
|||
return os.MkdirAll(p, perm)
|
||||
}
|
||||
|
||||
func (fx *Warehouse) Open(name string) (fs.File, error) {
|
||||
func (fx *Warehouse) Open(name string) (pkging.File, error) {
|
||||
name, err := fx.Abs(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -106,7 +106,7 @@ func (fx *Warehouse) Open(name string) (fs.File, error) {
|
|||
return NewFile(fx, f)
|
||||
}
|
||||
|
||||
func (f *Warehouse) Parse(p string) (fs.Path, error) {
|
||||
func (f *Warehouse) Parse(p string) (pkging.Path, error) {
|
||||
return f.paths.Parse(p)
|
||||
}
|
||||
|
||||
|
@ -134,7 +134,7 @@ func (f *Warehouse) Stat(name string) (os.FileInfo, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
info = fs.WithName(pt.Name, fs.NewFileInfo(info))
|
||||
info = pkging.WithName(pt.Name, pkging.NewFileInfo(info))
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return wf(pt.String(), fs.WithName(path, fs.NewFileInfo(fi)), nil)
|
||||
return wf(pt.String(), pkging.WithName(path, pkging.NewFileInfo(fi)), nil)
|
||||
})
|
||||
|
||||
return err
|
|
@ -0,0 +1,24 @@
|
|||
package hdware
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/pkging/waretest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Warehouse(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
mypkging, err := New()
|
||||
r.NoError(err)
|
||||
|
||||
mypkging.current.Dir = filepath.Join(mypkging.current.Dir, ".waretest")
|
||||
mypkging.paths.Current = mypkging.current
|
||||
|
||||
suite, err := waretest.NewSuite(mypkging)
|
||||
r.NoError(err)
|
||||
|
||||
suite.Test(t)
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package fs
|
||||
package pkging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package fs
|
||||
package pkging
|
||||
|
||||
import (
|
||||
"os"
|
|
@ -0,0 +1,12 @@
|
|||
package waretest
|
||||
|
||||
import (
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
type TestFile struct {
|
||||
Name string
|
||||
Path pkging.Path
|
||||
}
|
||||
|
||||
type TestFiles map[pkging.Path]TestFile
|
|
@ -1,10 +1,10 @@
|
|||
package fstest
|
||||
package waretest
|
||||
|
||||
import (
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
func Files(fx fs.Warehouse) (TestFiles, error) {
|
||||
func Files(fx pkging.Warehouse) (TestFiles, error) {
|
||||
tf := TestFiles{}
|
||||
for _, f := range fileList {
|
||||
pt, err := fx.Parse(f)
|
|
@ -1,4 +1,4 @@
|
|||
package fstest
|
||||
package waretest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -9,7 +9,7 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/fs"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
@ -18,12 +18,12 @@ const hart = "/easy/listening/grant.hart"
|
|||
const husker = "github.com/husker/du"
|
||||
|
||||
type Suite struct {
|
||||
fs.Warehouse
|
||||
pkging.Warehouse
|
||||
}
|
||||
|
||||
func NewSuite(yourfs fs.Warehouse) (Suite, error) {
|
||||
func NewSuite(yourpkging pkging.Warehouse) (Suite, error) {
|
||||
suite := Suite{
|
||||
Warehouse: yourfs,
|
||||
Warehouse: yourpkging,
|
||||
}
|
||||
return suite, nil
|
||||
}
|
||||
|
@ -141,17 +141,17 @@ func (s Suite) Test_Parse(t *testing.T) {
|
|||
ip := cur.ImportPath
|
||||
table := []struct {
|
||||
in string
|
||||
exp fs.Path
|
||||
exp pkging.Path
|
||||
}{
|
||||
{in: mould, exp: fs.Path{Pkg: ip, Name: mould}},
|
||||
{in: filepath.Join(cur.Dir, mould), exp: fs.Path{Pkg: ip, Name: mould}},
|
||||
{in: ":" + mould, exp: fs.Path{Pkg: ip, Name: mould}},
|
||||
{in: ip + ":" + mould, exp: fs.Path{Pkg: ip, Name: mould}},
|
||||
{in: ip, exp: fs.Path{Pkg: ip, Name: "/"}},
|
||||
{in: ":", exp: fs.Path{Pkg: ip, Name: "/"}},
|
||||
{in: husker + ":" + mould, exp: fs.Path{Pkg: husker, Name: mould}},
|
||||
{in: husker, exp: fs.Path{Pkg: husker, Name: "/"}},
|
||||
{in: husker + ":", exp: fs.Path{Pkg: husker, Name: "/"}},
|
||||
{in: mould, exp: pkging.Path{Pkg: ip, Name: mould}},
|
||||
{in: filepath.Join(cur.Dir, mould), exp: pkging.Path{Pkg: ip, Name: mould}},
|
||||
{in: ":" + mould, exp: pkging.Path{Pkg: ip, Name: mould}},
|
||||
{in: ip + ":" + mould, exp: pkging.Path{Pkg: ip, Name: mould}},
|
||||
{in: ip, exp: pkging.Path{Pkg: ip, Name: "/"}},
|
||||
{in: ":", exp: pkging.Path{Pkg: ip, Name: "/"}},
|
||||
{in: husker + ":" + mould, exp: pkging.Path{Pkg: husker, Name: mould}},
|
||||
{in: husker, exp: pkging.Path{Pkg: husker, Name: "/"}},
|
||||
{in: husker + ":", exp: pkging.Path{Pkg: husker, Name: "/"}},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
Loading…
Reference in New Issue