util: file path finder

Convenience to walk a directory tree looking for a file like
"ChangeLog.txt"

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2017-01-29 15:50:51 -05:00
parent 0cd4c2fee6
commit e646c9daef
Signed by: vbatts
GPG Key ID: 10937E57733F1362
2 changed files with 37 additions and 0 deletions

23
util/find.go Normal file
View File

@ -0,0 +1,23 @@
package util
import (
"os"
"path/filepath"
)
func FindFiles(root, name string) (paths []string, err error) {
paths = []string{}
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) == name {
paths = append(paths, path)
}
return nil
})
if err != nil {
return nil, err
}
return paths, nil
}

14
util/find_test.go Normal file
View File

@ -0,0 +1,14 @@
package util
import "testing"
func TestFind(t *testing.T) {
paths, err := FindFiles("../changelog", "ChangeLog.txt")
if err != nil {
t.Fatal(err)
}
if len(paths) != 1 {
t.Errorf("expected to find %d file, but found %d", 1, len(paths))
}
}