diff --git a/cmd/fs.go b/cmd/fs.go index 36109e8f..54a0dc2c 100644 --- a/cmd/fs.go +++ b/cmd/fs.go @@ -12,6 +12,7 @@ import ( "path" "path/filepath" "runtime" + "strings" "github.com/leaanthony/slicer" ) @@ -47,6 +48,22 @@ func (fs *FSHelper) FileExists(path string) bool { return fi.Mode().IsRegular() } +// FindFile returns the first occurrence of match inside path. +func (fs *FSHelper) FindFile(path, match string) (string, error) { + files, err := ioutil.ReadDir(path) + if err != nil { + return "", err + } + + for _, f := range files { + if !f.IsDir() && strings.Contains(f.Name(), match) { + return f.Name(), nil + } + } + + return "", fmt.Errorf("file not found") +} + // CreateFile creates a file at the given filename location with the contents // set to the given data. It will create intermediary directories if needed. func (fs *FSHelper) CreateFile(filename string, data []byte) error {