2024-07-05 02:37:59 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNotFound = errors.New("which: command not found")
|
|
|
|
|
2024-07-05 17:28:33 +02:00
|
|
|
func Which(cmd string) (dir string, err error) {
|
2024-07-05 02:37:59 +02:00
|
|
|
|
|
|
|
command := exec.Command("which", cmd)
|
|
|
|
|
|
|
|
outputBytes, err := command.Output()
|
|
|
|
if err != nil {
|
|
|
|
var exitErr *exec.ExitError
|
|
|
|
if errors.As(err, &exitErr) {
|
|
|
|
if exitErr.ExitCode() == 1 {
|
|
|
|
return "", ErrNotFound
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("command error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Trim(string(outputBytes), "\n"), nil
|
|
|
|
|
|
|
|
}
|