pkger/index.go

174 lines
2.9 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
import (
2019-07-31 18:53:36 +03:00
"encoding/json"
2019-07-31 00:21:26 +03:00
"fmt"
"os"
"path/filepath"
"strings"
2019-07-31 23:29:49 +03:00
"time"
2019-07-31 00:21:26 +03:00
"github.com/gobuffalo/here"
2019-08-01 19:03:12 +03:00
"github.com/markbates/pkger/paths"
2019-07-31 00:21:26 +03:00
"github.com/markbates/pkger/pkgs"
)
type index struct {
Pkg string
2019-08-01 19:03:12 +03:00
Files map[paths.Path]*File
2019-07-31 00:21:26 +03:00
}
2019-08-01 21:37:01 +03:00
func (i *index) Create(pt paths.Path) (*File, error) {
2019-07-31 23:29:49 +03:00
her, err := pkgs.Pkg(pt.Pkg)
if err != nil {
return nil, err
}
f := &File{
path: pt,
index: newIndex(),
her: her,
info: &FileInfo{
name: pt.Name,
mode: 0666,
modTime: time.Now(),
},
}
i.Files[pt] = f
return f, nil
}
2019-07-31 18:53:36 +03:00
func (i index) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{
"pkg": i.Pkg,
}
fm := map[string]File{}
for k, v := range i.Files {
fm[k.String()] = *v
}
m["files"] = fm
return json.Marshal(m)
}
2019-08-01 19:03:12 +03:00
func (i index) Walk(pt paths.Path, wf WalkFunc) error {
2019-07-31 00:21:26 +03:00
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
if len(i.Files) > 0 {
for k, v := range i.Files {
if k.Pkg != pt.Pkg {
continue
}
2019-08-01 19:03:12 +03:00
if err := wf(k, v.info); err != nil {
2019-07-31 00:21:26 +03:00
return err
}
}
}
var info here.Info
var err error
if pt.Pkg == "." {
info, err = pkgs.Current()
if err != nil {
return err
}
pt.Pkg = info.ImportPath
}
2019-08-01 19:03:12 +03:00
2019-07-31 00:21:26 +03:00
if info.IsZero() {
info, err = pkgs.Pkg(pt.Pkg)
if err != nil {
return fmt.Errorf("%s: %s", pt, err)
}
}
2019-08-01 19:03:12 +03:00
fp := filepath.Join(info.Dir, pt.Name)
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
2019-07-31 00:21:26 +03:00
if err != nil {
return err
}
path = strings.TrimPrefix(path, info.Dir)
2019-08-01 19:03:12 +03:00
pt, err := paths.Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
2019-07-31 00:21:26 +03:00
if err != nil {
return err
}
2019-08-01 19:03:12 +03:00
return wf(pt, NewFileInfo(fi))
2019-07-31 00:21:26 +03:00
})
return err
}
2019-08-01 19:03:12 +03:00
func (i index) Open(pt paths.Path) (*File, error) {
2019-07-31 00:21:26 +03:00
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
2019-08-01 21:37:01 +03:00
fmt.Printf("### index.go:110 pt.Pkg (%T) -> %q %+v\n", pt.Pkg, pt.Pkg, pt.Pkg)
fmt.Printf("### index.go:110 pt.Name (%T) -> %q %+v\n", pt.Name, pt.Name, pt.Name)
2019-07-31 00:21:26 +03:00
2019-08-01 21:37:01 +03:00
fmt.Printf("### index.go:112 i.Files (%T) -> %q %+v\n", i.Files, i.Files, i.Files)
2019-07-31 00:21:26 +03:00
f, ok := i.Files[pt]
if !ok {
return i.openDisk(pt)
}
return &File{
2019-07-31 23:29:49 +03:00
info: f.info,
path: f.path,
data: f.data,
her: f.her,
index: newIndex(),
2019-07-31 00:21:26 +03:00
}, nil
}
2019-08-01 19:03:12 +03:00
func (i index) openDisk(pt paths.Path) (*File, error) {
2019-07-31 00:21:26 +03:00
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
info, err := pkgs.Pkg(pt.Pkg)
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{
info: WithName(pt.Name, NewFileInfo(fi)),
her: info,
path: pt,
index: &index{
2019-08-01 19:03:12 +03:00
Files: map[paths.Path]*File{},
2019-07-31 00:21:26 +03:00
},
}
return f, nil
}
2019-08-01 19:03:12 +03:00
func (i index) Parse(p string) (paths.Path, error) {
pt, err := paths.Parse(p)
if err != nil {
return pt, err
2019-07-31 23:29:49 +03:00
}
2019-08-01 19:03:12 +03:00
2019-07-31 23:29:49 +03:00
if len(pt.Pkg) == 0 {
pt.Pkg = i.Pkg
}
2019-08-01 19:03:12 +03:00
2019-07-31 23:29:49 +03:00
return pt, nil
}
func newIndex() *index {
return &index{
2019-08-01 19:03:12 +03:00
Files: map[paths.Path]*File{},
2019-07-31 23:29:49 +03:00
}
2019-07-31 00:21:26 +03:00
}
2019-07-31 23:29:49 +03:00
var rootIndex = newIndex()