LookPath searches for an executable named file in the directories named by the PATH environment variable. If file contains a slash, it is tried directly and the PATH is not consulted. The result may be an absolute path or a path relative to the current directory.
(file string)
| 41 | // If file contains a slash, it is tried directly and the PATH is not consulted. |
| 42 | // The result may be an absolute path or a path relative to the current directory. |
| 43 | func LookPath(file string) (string, error) { |
| 44 | sep := string([]rune{os.PathSeparator}) |
| 45 | exts := findPathExtensions() |
| 46 | if strings.Contains(file, sep) { |
| 47 | path, err := findExecutable(file, exts) |
| 48 | if err == nil { |
| 49 | return path, nil |
| 50 | } |
| 51 | return "", exec.ErrNotFound |
| 52 | } |
| 53 | path := os.Getenv("PATH") |
| 54 | for _, dir := range filepath.SplitList(path) { |
| 55 | if dir == "" { |
| 56 | // Windows often has empty components in the PATH and |
| 57 | // treating them as "." is not expected. |
| 58 | if runtime.GOOS == "windows" { |
| 59 | continue |
| 60 | } |
| 61 | // Unix shell semantics: path element "" means "." |
| 62 | dir = "." |
| 63 | } |
| 64 | path := filepath.Join(dir, file) |
| 65 | if resolved, err := findExecutable(path, exts); err == nil { |
| 66 | return resolved, nil |
| 67 | } |
| 68 | } |
| 69 | return "", exec.ErrNotFound |
| 70 | } |
no test coverage detected