Created
January 21, 2018 19:41
-
-
Save picatz/97271e7c1501117b5cd8eea43899160c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
) | |
func EnviromentPaths() []string { | |
val, ok := os.LookupEnv("PATH") | |
if !ok { | |
return []string{} | |
} | |
return strings.Split(val, ":") | |
} | |
func Exists(path string) bool { | |
if _, err := os.Stat(path); err == nil { | |
return true | |
} | |
return false | |
} | |
func FindInPath(targets []string) <-chan string { | |
messages := make(chan string) | |
go func() { | |
defer close(messages) | |
var paths = EnviromentPaths() | |
for _, target := range targets { | |
for _, path := range paths { | |
var target = path + "/" + target | |
if Exists(target) { | |
messages <- target | |
} | |
} | |
} | |
}() | |
return messages | |
} | |
func main() { | |
if len(os.Args) > 1 { | |
found := FindInPath(os.Args[1:]) | |
for path := range found { | |
fmt.Println(path) | |
} | |
} else { | |
fmt.Println("usage: which program ...") | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment