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
|
|
|
)
|
|
|
|
|
|
|
|
type index struct {
|
2019-08-03 23:36:56 +03:00
|
|
|
Files *filesMap `json:"files"`
|
|
|
|
Infos *infosMap `json:"infos"`
|
|
|
|
Paths *pathsMap `json:"paths"`
|
|
|
|
Current here.Info `json:"current"`
|
2019-08-05 00:13:27 +03:00
|
|
|
once *sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *index) debug(key, format string, args ...interface{}) {
|
|
|
|
s := fmt.Sprintf(format, args...)
|
|
|
|
debug.Debug("[*index|%s|%s] %s", i.Current.ImportPath, key, s)
|
2019-08-02 07:22:17 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 23:36:56 +03:00
|
|
|
func (i *index) Parse(p string) (Path, error) {
|
2019-08-05 00:13:27 +03:00
|
|
|
i.debug("Parse", p)
|
2019-08-03 23:36:56 +03:00
|
|
|
pt, ok := i.Paths.Load(p)
|
|
|
|
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-02 07:22:17 +03:00
|
|
|
func (i *index) Info(p string) (here.Info, error) {
|
2019-08-02 21:51:09 +03:00
|
|
|
info, ok := i.Infos.Load(p)
|
|
|
|
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-02 21:51:09 +03:00
|
|
|
i.Infos.Store(p, info)
|
2019-08-02 07:22:17 +03:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2019-08-03 23:36:56 +03:00
|
|
|
func (i *index) Stat() (here.Info, error) {
|
2019-08-05 00:13:27 +03:00
|
|
|
var err error
|
2019-08-02 07:22:17 +03:00
|
|
|
i.once.Do(func() {
|
2019-08-05 00:13:27 +03:00
|
|
|
if i.Current.IsZero() {
|
|
|
|
i.Current, err = here.Current()
|
|
|
|
}
|
2019-08-02 07:22:17 +03:00
|
|
|
})
|
|
|
|
|
2019-08-05 00:13:27 +03:00
|
|
|
return i.Current, err
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
func (i *index) Create(pt Path) (*File, error) {
|
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-03 23:36:56 +03:00
|
|
|
if i.Files == nil {
|
|
|
|
i.Files = &filesMap{}
|
2019-08-02 22:14:11 +03:00
|
|
|
}
|
|
|
|
|
2019-08-03 23:36:56 +03:00
|
|
|
i.Files.Store(pt, f)
|
2019-08-09 04:51:58 +03:00
|
|
|
|
|
|
|
dir := Path{
|
|
|
|
Pkg: pt.Pkg,
|
|
|
|
Name: filepath.Dir(pt.Name),
|
|
|
|
}
|
|
|
|
i.MkdirAll(dir, 0644)
|
2019-08-03 23:36:56 +03:00
|
|
|
return f, nil
|
2019-07-31 18:53:36 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 00:13:27 +03:00
|
|
|
func (i *index) UnmarshalJSON(b []byte) error {
|
|
|
|
i.once = &sync.Once{}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
i.Infos = &infosMap{}
|
|
|
|
if err := json.Unmarshal(infos, i.Infos); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
files, ok := m["files"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("missing files")
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Files = &filesMap{}
|
|
|
|
if err := json.Unmarshal(files, i.Files); err != nil {
|
|
|
|
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 04:51:58 +03:00
|
|
|
i.Paths = &pathsMap{}
|
|
|
|
if err := json.Unmarshal(paths, i.Paths); 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 04:51:58 +03:00
|
|
|
if err := json.Unmarshal(current, &i.Current); err != nil {
|
|
|
|
return err
|
2019-08-02 06:21:37 +03:00
|
|
|
}
|
2019-08-09 04:51:58 +03:00
|
|
|
i.debug("UnmarshalJSON", "%v", i)
|
|
|
|
return nil
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
func (i index) openDisk(pt Path) (*File, error) {
|
2019-08-05 00:13:27 +03:00
|
|
|
i.debug("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 04:51:58 +03:00
|
|
|
info: WithName(pt.Name, NewFileInfo(fi)),
|
2019-07-31 00:21:26 +03:00
|
|
|
her: info,
|
|
|
|
path: pt,
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:29:49 +03:00
|
|
|
func newIndex() *index {
|
|
|
|
return &index{
|
2019-08-02 21:47:59 +03:00
|
|
|
Files: &filesMap{},
|
2019-08-02 21:51:09 +03:00
|
|
|
Infos: &infosMap{},
|
2019-08-03 23:36:56 +03:00
|
|
|
Paths: &pathsMap{},
|
2019-08-05 00:13:27 +03:00
|
|
|
once: &sync.Once{},
|
2019-07-31 23:29:49 +03:00
|
|
|
}
|
2019-07-31 00:21:26 +03:00
|
|
|
}
|
2019-07-31 23:29:49 +03:00
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
var rootIndex = func() *index {
|
|
|
|
i := newIndex()
|
|
|
|
return i
|
|
|
|
}()
|