Archived
1
0

Adds function to find directory of file on working path

This commit is contained in:
Jip J. Dekker 2016-03-17 17:10:32 +01:00
parent a23f6f21b9
commit 9201c7f360

View File

@ -15,6 +15,7 @@
package helpers
import (
"errors"
"os"
"path/filepath"
@ -52,3 +53,17 @@ func Exists(path string) bool {
}
return false
}
// FindFileDir returns the path of the
func FindFileDir(file string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", nil
}
for path := wd; path != "."; path = filepath.Dir(path) {
if Exists(filepath.Join(path, file)) {
return path, nil
}
}
return "", errors.New("directory not found")
}