pkger/walk.go

67 lines
1.1 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
2019-08-01 19:03:12 +03:00
import (
2019-08-09 04:51:58 +03:00
"fmt"
2019-08-01 19:03:12 +03:00
"os"
2019-08-09 04:51:58 +03:00
"path/filepath"
"strings"
"github.com/markbates/pkger/here"
2019-08-01 19:03:12 +03:00
)
2019-08-02 05:34:32 +03:00
type WalkFunc func(Path, os.FileInfo) error
2019-07-31 00:21:26 +03:00
func Walk(p string, wf WalkFunc) error {
2019-08-02 05:34:32 +03:00
pt, err := Parse(p)
2019-07-31 00:21:26 +03:00
if err != nil {
return err
}
2019-08-09 05:35:01 +03:00
filesCache.Range(func(k Path, v *File) bool {
2019-08-09 04:51:58 +03:00
if k.Pkg != pt.Pkg {
return true
}
if err = wf(k, v.info); err != nil {
if err == filepath.SkipDir {
return true
}
return false
}
return true
})
if err != nil {
return err
}
var info here.Info
if pt.Pkg == "." {
info, err = Stat()
if err != nil {
return err
}
pt.Pkg = info.ImportPath
}
if info.IsZero() {
info, err = Info(pt.Pkg)
if err != nil {
return fmt.Errorf("%s: %s", pt, err)
}
}
fp := filepath.Join(info.Dir, pt.Name)
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
path = strings.TrimPrefix(path, info.Dir)
pt, err := Parse(fmt.Sprintf("%s:%s", pt.Pkg, path))
if err != nil {
return err
}
return wf(pt, NewFileInfo(fi))
})
return err
}