pkger/index.go

173 lines
2.9 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
import (
2019-08-05 00:13:27 +03:00
"encoding/json"
2019-07-31 00:21:26 +03:00
"fmt"
"os"
"path/filepath"
"strings"
2019-08-02 07:22:17 +03:00
"sync"
2019-07-31 23:29:49 +03:00
"time"
2019-07-31 00:21:26 +03:00
2019-08-05 00:13:27 +03:00
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/internal/debug"
2019-07-31 00:21:26 +03:00
)
2019-08-09 05:35:01 +03:00
var filesCache = &filesMap{}
var infosCache = &infosMap{}
var pathsCache = &pathsMap{}
var curOnce = &sync.Once{}
var currentInfo here.Info
2019-08-05 00:13:27 +03:00
2019-08-09 05:35:01 +03:00
func dubeg(key, format string, args ...interface{}) {
2019-08-05 00:13:27 +03:00
s := fmt.Sprintf(format, args...)
2019-08-09 05:35:01 +03:00
debug.Debug("[%s|%s] %s", key, s)
2019-08-02 07:22:17 +03:00
}
2019-08-09 05:35:01 +03:00
func Parse(p string) (Path, error) {
dubeg("Parse", p)
pt, ok := pathsCache.Load(p)
2019-08-03 23:36:56 +03:00
if ok {
return pt, nil
}
if len(p) == 0 {
return build(p, "", "")
}
res := pathrx.FindAllStringSubmatch(p, -1)
if len(res) == 0 {
return pt, fmt.Errorf("could not parse %q", p)
}
matches := res[0]
if len(matches) != 4 {
return pt, fmt.Errorf("could not parse %q", p)
}
return build(p, matches[1], matches[3])
}
2019-08-09 05:35:01 +03:00
func Info(p string) (here.Info, error) {
info, ok := infosCache.Load(p)
2019-08-02 21:51:09 +03:00
if ok {
2019-08-02 07:22:17 +03:00
return info, nil
}
2019-08-05 00:13:27 +03:00
info, err := here.Package(p)
2019-08-02 07:22:17 +03:00
if err != nil {
return info, err
}
2019-08-09 05:35:01 +03:00
infosCache.Store(p, info)
2019-08-02 07:22:17 +03:00
return info, nil
}
2019-08-09 05:35:01 +03:00
func Stat() (here.Info, error) {
2019-08-05 00:13:27 +03:00
var err error
2019-08-09 05:35:01 +03:00
curOnce.Do(func() {
if currentInfo.IsZero() {
currentInfo, err = here.Current()
2019-08-05 00:13:27 +03:00
}
2019-08-02 07:22:17 +03:00
})
2019-08-09 05:35:01 +03:00
return currentInfo, err
2019-07-31 00:21:26 +03:00
}
2019-08-09 05:35:01 +03:00
func Create(name string) (*File, error) {
pt, err := Parse(name)
if err != nil {
return nil, err
}
2019-08-02 06:21:37 +03:00
her, err := Info(pt.Pkg)
2019-07-31 23:29:49 +03:00
if err != nil {
return nil, err
}
f := &File{
2019-08-02 05:34:32 +03:00
path: pt,
her: her,
2019-07-31 23:29:49 +03:00
info: &FileInfo{
2019-08-02 00:35:42 +03:00
name: strings.TrimPrefix(pt.Name, "/"),
2019-07-31 23:29:49 +03:00
mode: 0666,
modTime: time.Now(),
2019-08-09 04:51:58 +03:00
virtual: true,
2019-07-31 23:29:49 +03:00
},
}
2019-08-09 05:35:01 +03:00
filesCache.Store(pt, f)
2019-08-09 04:51:58 +03:00
2019-08-09 05:35:01 +03:00
if err := MkdirAll(filepath.Dir(pt.Name), 0644); err != nil {
return nil, err
2019-08-09 04:51:58 +03:00
}
2019-08-03 23:36:56 +03:00
return f, nil
2019-07-31 18:53:36 +03:00
}
2019-08-09 05:35:01 +03:00
func UnmarshalJSON(b []byte) error {
2019-08-05 00:13:27 +03:00
m := map[string]json.RawMessage{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
infos, ok := m["infos"]
if !ok {
return fmt.Errorf("missing infos")
}
2019-08-09 05:35:01 +03:00
infosCache = &infosMap{}
if err := json.Unmarshal(infos, infosCache); err != nil {
2019-08-05 00:13:27 +03:00
return err
}
files, ok := m["files"]
if !ok {
return fmt.Errorf("missing files")
}
2019-08-09 05:35:01 +03:00
filesCache = &filesMap{}
if err := json.Unmarshal(files, filesCache); err != nil {
2019-08-05 00:13:27 +03:00
return err
}
2019-08-09 04:51:58 +03:00
paths, ok := m["paths"]
2019-08-05 00:13:27 +03:00
if !ok {
2019-08-09 04:51:58 +03:00
return fmt.Errorf("missing paths")
2019-08-05 00:13:27 +03:00
}
2019-08-02 07:22:17 +03:00
2019-08-09 05:35:01 +03:00
pathsCache = &pathsMap{}
if err := json.Unmarshal(paths, pathsCache); err != nil {
2019-08-02 21:47:59 +03:00
return err
2019-07-31 00:21:26 +03:00
}
2019-08-09 04:51:58 +03:00
current, ok := m["current"]
2019-07-31 00:21:26 +03:00
if !ok {
2019-08-09 04:51:58 +03:00
return fmt.Errorf("missing current")
2019-07-31 00:21:26 +03:00
}
2019-08-09 05:35:01 +03:00
if err := json.Unmarshal(current, &currentInfo); err != nil {
2019-08-09 04:51:58 +03:00
return err
2019-08-02 06:21:37 +03:00
}
2019-08-09 04:51:58 +03:00
return nil
2019-07-31 00:21:26 +03:00
}
2019-08-09 05:35:01 +03:00
func openDisk(pt Path) (*File, error) {
dubeg("openDisk", pt.String())
2019-08-02 06:21:37 +03:00
info, err := Info(pt.Pkg)
2019-07-31 00:21:26 +03:00
if err != nil {
return nil, err
}
fp := info.Dir
if len(pt.Name) > 0 {
fp = filepath.Join(fp, pt.Name)
}
fi, err := os.Stat(fp)
if err != nil {
return nil, err
}
f := &File{
2019-08-09 05:35:01 +03:00
info: WithName(strings.TrimPrefix(pt.Name, "/"), NewFileInfo(fi)),
2019-07-31 00:21:26 +03:00
her: info,
path: pt,
}
return f, nil
}