2016-03-16 19:59:34 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExcludeFunc is the type of function called on each path walked to determine
|
|
|
|
// whether to be excluded from the assembled DirectoryHierarchy. If the func
|
|
|
|
// returns true, then the path is not included in the spec.
|
|
|
|
type ExcludeFunc func(path string, info os.FileInfo) bool
|
|
|
|
|
|
|
|
//
|
|
|
|
// To be able to do a "walk" that produces an outcome with `/set ...` would
|
|
|
|
// need a more linear walk, which this can not ensure.
|
|
|
|
func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHierarchy, error) {
|
|
|
|
dh := DirectoryHierarchy{}
|
2016-03-17 17:54:59 +00:00
|
|
|
count := 0
|
|
|
|
// TODO insert signature and metadata comments first
|
2016-03-16 19:59:34 +00:00
|
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ex := range exlcudes {
|
|
|
|
if ex(path, info) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e := Entry{}
|
|
|
|
//e.Name = filepath.Base(path)
|
|
|
|
e.Name = path
|
2016-03-17 17:54:59 +00:00
|
|
|
e.Pos = count
|
2016-03-16 19:59:34 +00:00
|
|
|
for _, keyword := range keywords {
|
|
|
|
if str, err := KeywordFuncs[keyword](path, info); err == nil && str != "" {
|
|
|
|
e.Keywords = append(e.Keywords, str)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dh.Entries = append(dh.Entries, e)
|
2016-03-17 17:54:59 +00:00
|
|
|
count++
|
2016-03-16 19:59:34 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return &dh, err
|
|
|
|
}
|