1 Star 0 Fork 0

mwdworks/packr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
walk.go 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Yonghwan SO 提交于 2021-10-30 20:53 . moved v2 files from v2/ to /
package packr
import (
"sort"
"strings"
"github.com/gobuffalo/packd"
"github.com/gobuffalo/packr/v2/file"
"github.com/gobuffalo/packr/v2/file/resolver"
"github.com/gobuffalo/packr/v2/plog"
)
// WalkFunc is used to walk a box
type WalkFunc = packd.WalkFunc
// Walk will traverse the box and call the WalkFunc for each file in the box/folder.
func (b *Box) Walk(wf WalkFunc) error {
m := map[string]file.File{}
dr := b.DefaultResolver
if dr == nil {
cd := resolver.OsPath(b.ResolutionDir)
dr = &resolver.Disk{Root: cd}
}
if fm, ok := dr.(file.FileMappable); ok {
for n, f := range fm.FileMap() {
m[n] = f
}
}
var err error
b.resolvers.Range(func(n string, r resolver.Resolver) bool {
var f file.File
f, err = r.Resolve("", n)
if err != nil {
return false
}
keep := true
for k := range m {
if strings.EqualFold(k, n) {
keep = false
}
}
if keep {
m[n] = f
}
return true
})
if err != nil {
return err
}
var keys = make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
osPath := resolver.OsPath(k)
plog.Debug(b, "Walk", "path", k, "osPath", osPath)
if err := wf(osPath, m[k]); err != nil {
return err
}
}
return nil
}
// WalkPrefix will call box.Walk and call the WalkFunc when it finds paths that have a matching prefix
func (b *Box) WalkPrefix(prefix string, wf WalkFunc) error {
ipref := resolver.OsPath(prefix)
return b.Walk(func(path string, f File) error {
ipath := resolver.OsPath(path)
if strings.HasPrefix(ipath, ipref) {
if err := wf(path, f); err != nil {
return err
}
}
return nil
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mwdworks/packr.git
git@gitee.com:mwdworks/packr.git
mwdworks
packr
packr
master

搜索帮助