File: executable.go

package info (click to toggle)
pat 0.19.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,228 kB
  • sloc: javascript: 3,864; sh: 147; makefile: 11
file content (41 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download
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
package propagation

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

// findExecutable finds the given executable name and returns an absolute path.
// The lookup order is:
// 1. If 'name' is an absolute path, it is used directly.
// 2. If 'name' contains a path separator, it's treated as a relative path to the current working directory.
// 3. If 'name' does not contain a separator, it is looked up in PATH (using exec.LookPath).
func findExecutable(name string) (string, error) {
	name = os.ExpandEnv(name)

	// 1. If 'name' is an absolute path, check if it exists and return it.
	if filepath.IsAbs(name) {
		if _, err := os.Stat(name); err != nil {
			return "", err
		}
		return name, nil
	}

	// 2. If 'name' contains a path separator, treat as a relative path.
	if strings.ContainsRune(name, os.PathSeparator) {
		absPath, err := filepath.Abs(name)
		if err != nil {
			return "", fmt.Errorf("failed to get absolute path for '%s': %w", name, err)
		}
		if _, err := os.Stat(absPath); err != nil {
			return "", err
		}
		return absPath, nil
	}

	// 3. Try to find the command in PATH.
	return exec.LookPath(name) // LookPath returns an absolute path or an error
}