From 7c7c168c3d7f5eed7fc8e6dee5d3e3cdab3fa457 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 9 Jan 2020 23:05:03 +0800 Subject: [PATCH] improve gproc.SearchBinary --- .example/other/test.go | 2 +- os/gproc/gproc.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.example/other/test.go b/.example/other/test.go index e9d54731a..2641c4e3a 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -6,7 +6,7 @@ import ( ) func main() { - g.Dump(gfile.ScanDirFile("/Users/john/Temp/test", "*", true)) + g.Dump(gfile.ScanDirFile("/Users/john/Temp/test", "Dockerfile", true)) //if err := gfile.ReplaceDir("gf-empty", "app", "/Users/john/Temp/test", "*.*", true); err != nil { // glog.Fatal("content replacing failed,", err.Error()) //} diff --git a/os/gproc/gproc.go b/os/gproc/gproc.go index bfc8839db..38c9751ca 100644 --- a/os/gproc/gproc.go +++ b/os/gproc/gproc.go @@ -191,18 +191,23 @@ func SearchBinary(file string) string { array := ([]string)(nil) switch runtime.GOOS { case "windows": - array = gstr.SplitAndTrim(os.Getenv("Path"), ";") + envPath := genv.Get("PATH", genv.Get("Path")) + if gstr.Contains(envPath, ";") { + array = gstr.SplitAndTrim(envPath, ";") + } else if gstr.Contains(envPath, ":") { + array = gstr.SplitAndTrim(envPath, ":") + } if gfile.Ext(file) != ".exe" { file += ".exe" } default: - array = gstr.SplitAndTrim(os.Getenv("PATH"), ":") + array = gstr.SplitAndTrim(genv.Get("PATH"), ":") } if len(array) > 0 { path := "" for _, v := range array { path = v + gfile.Separator + file - if gfile.Exists(path) { + if gfile.Exists(path) && gfile.IsFile(path) { return path } }