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