Skip to content

Commit

Permalink
Lookup executable name in PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed Aug 1, 2014
1 parent 0815037 commit 2a1feab
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,34 @@ func searchpaths() (s []string) {
return
}

func splitdirpkg(a []string) (dirs, pkgs []string) {
func splitdirpkgexe(a []string) (dirs, pkgs, exes []string) {
var (
appenddirs = appenduniq(&dirs)
appendpkgs = appenduniq(&pkgs)
appendexes = appenduniq(&exes)
)
LOOP:
for _, a := range a {
switch {
case a == ".":
if wd, err := os.Getwd(); err == nil {
appenddirs(wd)
}
default:
if fi, err := os.Stat(a); err == nil {
if !fi.IsDir() {
a = filepath.Dir(a)
if strings.Contains(a, string(os.PathSeparator)) {
if fi, err := os.Stat(a); err == nil {
if !fi.IsDir() {
a = filepath.Dir(a)
}
appenddirs(a)
continue LOOP
}
}
if IsExecutable(a) || !strings.Contains(a, ".") {
if path, err := exec.LookPath(a); err == nil {
appendexes(path)
}
appenddirs(a)
} else if os.IsNotExist(err) {
} else {
appendpkgs(a)
}
}
Expand Down Expand Up @@ -216,7 +226,6 @@ func Source(b []Bin, gopath string) error {
}
cmd.Env = environ(gopath, filepath.Join(gopath, "bin"))
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
fmt.Println(cmd.Args)
return cmd.Run()
}

Expand Down Expand Up @@ -364,12 +373,11 @@ func searchSymlink(args []string, symlink bool) (b []Bin, s map[string][]Bin, er
sch chan skv
bs map[string]Bin
)
dirs, pkgs := splitdirpkg(args)
if dirs == nil || len(dirs) == 0 {
dirs = searchpaths()
}
if dirs == nil || len(dirs) == 0 {
return nil, nil, errors.New("bin: couldn't find any search paths")
dirs, pkgs, exes := splitdirpkgexe(args)
if len(pkgs) == 0 && len(exes) == 0 && len(dirs) == 0 {
if dirs = searchpaths(); len(dirs) == 0 {
return nil, nil, errors.New("bin: couldn't find any search paths")
}
}
if symlink {
s, sch, bs = make(map[string][]Bin), make(chan skv), make(map[string]Bin)
Expand All @@ -385,7 +393,11 @@ func searchSymlink(args []string, symlink bool) (b []Bin, s map[string][]Bin, er
}()
}
// TODO(rjeczalik): cap(ch) = max(count files in dirs)
ch, wg := make(chan binpath, 128), sync.WaitGroup{}
ch, wg := make(chan binpath, max(len(exes), 128)), sync.WaitGroup{}
for _, exe := range exes {
wg.Add(1)
ch <- binpath{path: exe, canwrite: CanWrite(exe)}
}
for i := 0; i < parallel; i++ {
go func() {
for p := range ch {
Expand Down

0 comments on commit 2a1feab

Please sign in to comment.