mirror of
https://github.com/vbatts/sl-feeds.git
synced 2025-02-27 16:29:01 +00:00
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:
parent
0cd4c2fee6
commit
e646c9daef
2 changed files with 37 additions and 0 deletions
23
util/find.go
Normal file
23
util/find.go
Normal 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
14
util/find_test.go
Normal 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))
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue