-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd_entries.go
48 lines (40 loc) · 900 Bytes
/
cmd_entries.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
package main
import (
"fmt"
"os"
"strings"
)
var PathEntries = strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
func SearchCmdEntries(query string) (list LaunchEntriesList) {
query = strings.TrimSpace(query)
if query == "" {
return nil
}
cmd := SplitCommandline(query)
if len(cmd) == 0 {
return nil
}
if !IsInHistory(query) && IsCommand(cmd[0]) {
return LaunchEntriesList{NewEntryFromCommand(query)}
}
return nil
}
func IsCommand(cmd string) bool {
return IsPathExecutable(cmd) || IsCommandInPath(cmd)
}
func IsPathExecutable(path string) bool {
stat, err := os.Stat(path)
if err != nil {
return false
}
return IsExecutable(stat)
}
func IsCommandInPath(cmd string) bool {
for _, path := range PathEntries {
filePath := fmt.Sprintf("%v%v%v", path, string(os.PathSeparator), cmd)
if IsPathExecutable(filePath) {
return true
}
}
return false
}