|
@@ -1,9 +1,12 @@
|
1
|
1
|
package jsh
|
2
|
2
|
|
3
|
3
|
import (
|
|
4
|
+ "errors"
|
|
5
|
+ "fmt"
|
4
|
6
|
"math"
|
5
|
7
|
"os"
|
6
|
8
|
"os/exec"
|
|
9
|
+ "strings"
|
7
|
10
|
"unicode"
|
8
|
11
|
)
|
9
|
12
|
|
|
@@ -52,7 +55,13 @@ func FallbackWithArgs(program string, args []string) *[]byte {
|
52
|
55
|
if args == nil {
|
53
|
56
|
args = os.Args[1:]
|
54
|
57
|
}
|
55
|
|
- out, err := exec.Command(program, args...).Output()
|
|
58
|
+ executable, err := findExecutable(program)
|
|
59
|
+
|
|
60
|
+ if err != nil {
|
|
61
|
+ panic(err)
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ out, err := exec.Command(executable, args...).Output()
|
56
|
65
|
if err != nil {
|
57
|
66
|
panic(err)
|
58
|
67
|
}
|
|
@@ -62,3 +71,58 @@ func FallbackWithArgs(program string, args []string) *[]byte {
|
62
|
71
|
func Fallback(program string) *[]byte {
|
63
|
72
|
return FallbackWithArgs(program, nil)
|
64
|
73
|
}
|
|
74
|
+
|
|
75
|
+// Finds a given executable on the system path, returning the absolute path the program if found, and an error
|
|
76
|
+// if it's not found
|
|
77
|
+func findExecutable(programName string) (string, error) {
|
|
78
|
+ // First, find our binary location
|
|
79
|
+ binLocWithName, err := os.Readlink("/proc/self/exe")
|
|
80
|
+ if err != nil {
|
|
81
|
+ return "", err
|
|
82
|
+ }
|
|
83
|
+ // Get our binary name and remove it from the binary path, leaving just our binary location
|
|
84
|
+ // Also trim the extra path separator
|
|
85
|
+ binName := os.Args[0]
|
|
86
|
+ binLoc := strings.TrimSuffix(binLocWithName, binName)
|
|
87
|
+ binLoc = strings.TrimSuffix(binLoc, string(os.PathSeparator))
|
|
88
|
+
|
|
89
|
+ // Get the system path and split it by the system path separator
|
|
90
|
+ pathString := os.Getenv("PATH")
|
|
91
|
+ path := strings.Split(pathString, string(os.PathListSeparator))
|
|
92
|
+
|
|
93
|
+ // Loop until we find our location on the path, so we know where to start looking after for the
|
|
94
|
+ // next implementation of the given program name
|
|
95
|
+ var foundPath bool
|
|
96
|
+ var pathNum int
|
|
97
|
+ for index, pathEl := range path {
|
|
98
|
+ // Trim the path separator from this element, if it has it
|
|
99
|
+ pathEl = strings.TrimSuffix(pathEl, string(os.PathSeparator))
|
|
100
|
+ if pathEl == binLoc {
|
|
101
|
+ foundPath = true
|
|
102
|
+ pathNum = index
|
|
103
|
+ break
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ // If we found our path on the real path, then search the remaining path elements. Otherwise, search all elements
|
|
108
|
+ if foundPath {
|
|
109
|
+ return searchPath(programName, path[pathNum+1:len(path)-1])
|
|
110
|
+ } else {
|
|
111
|
+ return searchPath(programName, path)
|
|
112
|
+ }
|
|
113
|
+}
|
|
114
|
+
|
|
115
|
+// Searches the given set of paths for a the given program, returing when it finds it or returning an error if the
|
|
116
|
+// program is not found
|
|
117
|
+func searchPath(programName string, path []string) (string, error) {
|
|
118
|
+ // Loop through each path element, test for the existance of the file, and then return if we find it
|
|
119
|
+ for _, pathEl := range path {
|
|
120
|
+ fullProgram := pathEl + string(os.PathSeparator) + programName
|
|
121
|
+ _, err := os.Stat(fullProgram)
|
|
122
|
+ if err == nil {
|
|
123
|
+ return fullProgram, nil
|
|
124
|
+ }
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ return "", errors.New(fmt.Sprintf("Could not find program %s on the PATH: %s", programName, path))
|
|
128
|
+}
|