pkger/pkging/osware/warehouse.go

180 lines
3.1 KiB
Go
Raw Normal View History

2019-09-02 00:58:20 +03:00
package hdware
2019-09-01 00:00:24 +03:00
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/maps"
2019-09-02 01:02:45 +03:00
"github.com/markbates/pkger/pkging"
2019-09-01 00:00:24 +03:00
)
2019-09-02 01:02:45 +03:00
var _ pkging.Warehouse = &Warehouse{}
2019-09-01 00:00:24 +03:00
2019-09-02 00:54:05 +03:00
type Warehouse struct {
2019-09-01 00:00:24 +03:00
infos *maps.Infos
paths *maps.Paths
current here.Info
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) Abs(p string) (string, error) {
2019-09-01 23:18:39 +03:00
pt, err := f.Parse(p)
if err != nil {
return "", err
}
return f.AbsPath(pt)
}
2019-09-02 01:02:45 +03:00
func (f *Warehouse) AbsPath(pt pkging.Path) (string, error) {
2019-09-01 23:18:39 +03:00
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
}
2019-09-02 00:54:05 +03:00
func New() (*Warehouse, error) {
2019-09-01 06:29:25 +03:00
info, err := here.Current()
if err != nil {
return nil, err
2019-09-01 00:00:24 +03:00
}
2019-09-02 00:54:05 +03:00
return &Warehouse{
2019-09-01 06:29:25 +03:00
infos: &maps.Infos{},
paths: &maps.Paths{
Current: info,
},
current: info,
}, nil
2019-09-01 00:00:24 +03:00
}
2019-09-02 01:02:45 +03:00
func (fx *Warehouse) Create(name string) (pkging.File, error) {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
f, err := os.Create(name)
if err != nil {
return nil, err
}
return NewFile(fx, f)
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) Current() (here.Info, error) {
2019-09-01 00:00:24 +03:00
return f.current, nil
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) Info(p string) (here.Info, error) {
2019-09-01 00:00:24 +03:00
info, ok := f.infos.Load(p)
if ok {
return info, nil
}
info, err := here.Package(p)
if err != nil {
return info, err
}
f.infos.Store(p, info)
return info, nil
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) MkdirAll(p string, perm os.FileMode) error {
2019-09-01 23:18:39 +03:00
p, err := f.Abs(p)
2019-09-01 00:00:24 +03:00
if err != nil {
return err
}
return os.MkdirAll(p, perm)
}
2019-09-02 01:02:45 +03:00
func (fx *Warehouse) Open(name string) (pkging.File, error) {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
f, err := os.Open(name)
if err != nil {
return nil, err
}
return NewFile(fx, f)
}
2019-09-02 01:02:45 +03:00
func (f *Warehouse) Parse(p string) (pkging.Path, error) {
2019-09-01 00:00:24 +03:00
return f.paths.Parse(p)
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) ReadFile(s string) ([]byte, error) {
2019-09-01 23:18:39 +03:00
s, err := f.Abs(s)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
return ioutil.ReadFile(s)
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) Stat(name string) (os.FileInfo, error) {
2019-09-01 23:27:31 +03:00
pt, err := f.Parse(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
2019-09-01 23:27:31 +03:00
abs, err := f.AbsPath(pt)
if err != nil {
return nil, err
}
info, err := os.Stat(abs)
if err != nil {
return nil, err
}
2019-09-02 01:02:45 +03:00
info = pkging.WithName(pt.Name, pkging.NewFileInfo(info))
2019-09-01 23:27:31 +03:00
return info, nil
2019-09-01 00:00:24 +03:00
}
2019-09-02 00:54:05 +03:00
func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error {
2019-09-01 23:18:39 +03:00
fp, err := f.Abs(p)
2019-09-01 00:00:24 +03:00
if err != nil {
return err
}
pt, err := f.Parse(p)
if err != nil {
return err
}
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
path = strings.TrimPrefix(path, fp)
pt, err := f.Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
if err != nil {
return err
}
2019-09-02 01:02:45 +03:00
return wf(pt.String(), pkging.WithName(path, pkging.NewFileInfo(fi)), nil)
2019-09-01 00:00:24 +03:00
})
return err
}
2019-09-02 00:54:05 +03:00
func (fx *Warehouse) Remove(name string) error {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 05:45:22 +03:00
if err != nil {
return err
}
return os.Remove(name)
}
2019-09-02 00:54:05 +03:00
func (fx *Warehouse) RemoveAll(name string) error {
2019-09-01 23:18:39 +03:00
name, err := fx.Abs(name)
2019-09-01 05:45:22 +03:00
if err != nil {
return err
}
return os.RemoveAll(name)
}