forked from izumin5210/grapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
47 lines (38 loc) · 749 Bytes
/
exec.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
package fs
import (
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/spf13/afero"
)
func ListExecutableWithPrefix(fs afero.Fs, prefix string) []string {
var wg sync.WaitGroup
ch := make(chan string)
for _, path := range filepath.SplitList(os.Getenv("PATH")) {
wg.Add(1)
go func(path string) {
defer wg.Done()
files, err := afero.ReadDir(fs, path)
if err != nil {
return
}
for _, f := range files {
if m := f.Mode(); !f.IsDir() && m&0111 != 0 && strings.HasPrefix(f.Name(), prefix) {
ch <- f.Name()
}
}
}(path)
}
go func() {
wg.Wait()
close(ch)
}()
execs := make([]string, 0, 100)
for exec := range ch {
execs = append(execs, exec)
}
sort.Strings(execs)
return execs
}