-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_exec_path.go
49 lines (44 loc) · 1.03 KB
/
get_exec_path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package xpath
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"github.com/hashicorp/go-multierror"
)
func GetExecPath(
execPathUnprocessed string,
tryDirs ...string,
) (string, error) {
var result *multierror.Error
for _, relPath := range append([]string{""}, tryDirs...) {
r, err := getExecPath(execPathUnprocessed, relPath)
if err == nil {
return r, nil
}
result = multierror.Append(result, fmt.Errorf("unable to locate exec path of '%s' in '%s': %w", execPathUnprocessed, relPath, err))
}
return "", result
}
func getExecPath(
execPathUnprocessed string,
dir string,
) (string, error) {
if dir != "" {
execPathUnprocessed = path.Join(dir, execPathUnprocessed)
}
execPath, err := exec.LookPath(execPathUnprocessed)
switch {
case err == nil:
return execPath, nil
case errors.Is(err, exec.ErrDot):
wd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("unable to get current working directory: %w", err)
}
return exec.LookPath(path.Join(wd, execPathUnprocessed))
default:
return "", err
}
}