SearchPath will check if a file at the given path exists or not. If it does, it will return the path to it. If it does not, it will search for any files at the given path with any of the given possible names. If any of these match a file, the first matching path will be returned. If no files are fou
(path string, possibleFilenames []string)
| 120 | // a file, the first matching path will be returned. If no files are found, an |
| 121 | // error will be returned. |
| 122 | func SearchPath(path string, possibleFilenames []string) (string, error) { |
| 123 | // Get file info about the path |
| 124 | fi, err := os.Stat(path) |
| 125 | if err != nil { |
| 126 | return "", err |
| 127 | } |
| 128 | |
| 129 | // If the path exists and is a regular file, device, symlink, or named pipe, |
| 130 | // return the absolute path to it |
| 131 | if fi.Mode().IsRegular() || |
| 132 | fi.Mode()&os.ModeDevice != 0 || |
| 133 | fi.Mode()&os.ModeSymlink != 0 || |
| 134 | fi.Mode()&os.ModeNamedPipe != 0 { |
| 135 | return filepath.Abs(path) |
| 136 | } |
| 137 | |
| 138 | // If the path is a directory, check if any of the possible names exist |
| 139 | // in that directory |
| 140 | for _, filename := range possibleFilenames { |
| 141 | alt := filepathext.SmartJoin(path, filename) |
| 142 | if _, err := os.Stat(alt); err == nil { |
| 143 | return filepath.Abs(alt) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return "", os.ErrNotExist |
| 148 | } |
| 149 | |
| 150 | // SearchPathRecursively walks up the directory tree starting at the given |
| 151 | // path, calling the Search function in each directory until it finds a matching |
no test coverage detected
searching dependent graphs…