mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
os/exec: cleanup and remove duplicated code
Change-Id: Ia2f61427b1cc09064ac4c0563bccbd9b98767a0e Reviewed-on: https://go-review.googlesource.com/18118 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
c1e8892060
commit
80423f1e64
4 changed files with 29 additions and 60 deletions
|
|
@ -7,6 +7,7 @@ package exec
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -44,9 +45,10 @@ func LookPath(file string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
path := os.Getenv("path")
|
path := os.Getenv("path")
|
||||||
for _, dir := range strings.Split(path, "\000") {
|
for _, dir := range filepath.SplitList(path) {
|
||||||
if err := findExecutable(dir + "/" + file); err == nil {
|
path := filepath.Join(dir, file)
|
||||||
return dir + "/" + file, nil
|
if err := findExecutable(path); err == nil {
|
||||||
|
return path, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", &Error{file, ErrNotFound}
|
return "", &Error{file, ErrNotFound}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ package exec
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -42,16 +43,13 @@ func LookPath(file string) (string, error) {
|
||||||
}
|
}
|
||||||
return "", &Error{file, err}
|
return "", &Error{file, err}
|
||||||
}
|
}
|
||||||
pathenv := os.Getenv("PATH")
|
path := os.Getenv("PATH")
|
||||||
if pathenv == "" {
|
for _, dir := range filepath.SplitList(path) {
|
||||||
return "", &Error{file, ErrNotFound}
|
|
||||||
}
|
|
||||||
for _, dir := range strings.Split(pathenv, ":") {
|
|
||||||
if dir == "" {
|
if dir == "" {
|
||||||
// Unix shell semantics: path element "" means "."
|
// Unix shell semantics: path element "" means "."
|
||||||
dir = "."
|
dir = "."
|
||||||
}
|
}
|
||||||
path := dir + "/" + file
|
path := filepath.Join(dir, file)
|
||||||
if err := findExecutable(path); err == nil {
|
if err := findExecutable(path); err == nil {
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ package exec
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -56,20 +57,22 @@ func findExecutable(file string, exts []string) (string, error) {
|
||||||
// a suitable candidate.
|
// a suitable candidate.
|
||||||
// The result may be an absolute path or a path relative to the current directory.
|
// The result may be an absolute path or a path relative to the current directory.
|
||||||
func LookPath(file string) (string, error) {
|
func LookPath(file string) (string, error) {
|
||||||
|
var exts []string
|
||||||
x := os.Getenv(`PATHEXT`)
|
x := os.Getenv(`PATHEXT`)
|
||||||
if x == "" {
|
if x != "" {
|
||||||
x = `.COM;.EXE;.BAT;.CMD`
|
for _, e := range strings.Split(strings.ToLower(x), `;`) {
|
||||||
}
|
if e == "" {
|
||||||
exts := []string{}
|
continue
|
||||||
for _, e := range strings.Split(strings.ToLower(x), `;`) {
|
}
|
||||||
if e == "" {
|
if e[0] != '.' {
|
||||||
continue
|
e = "." + e
|
||||||
|
}
|
||||||
|
exts = append(exts, e)
|
||||||
}
|
}
|
||||||
if e[0] != '.' {
|
} else {
|
||||||
e = "." + e
|
exts = []string{".com", ".exe", ".bat", ".cmd"}
|
||||||
}
|
|
||||||
exts = append(exts, e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.ContainsAny(file, `:\/`) {
|
if strings.ContainsAny(file, `:\/`) {
|
||||||
if f, err := findExecutable(file, exts); err == nil {
|
if f, err := findExecutable(file, exts); err == nil {
|
||||||
return f, nil
|
return f, nil
|
||||||
|
|
@ -77,48 +80,14 @@ func LookPath(file string) (string, error) {
|
||||||
return "", &Error{file, err}
|
return "", &Error{file, err}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f, err := findExecutable(`.\`+file, exts); err == nil {
|
if f, err := findExecutable(filepath.Join(".", file), exts); err == nil {
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
if pathenv := os.Getenv(`PATH`); pathenv != "" {
|
path := os.Getenv("path")
|
||||||
for _, dir := range splitList(pathenv) {
|
for _, dir := range filepath.SplitList(path) {
|
||||||
if f, err := findExecutable(dir+`\`+file, exts); err == nil {
|
if f, err := findExecutable(filepath.Join(dir, file), exts); err == nil {
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", &Error{file, ErrNotFound}
|
return "", &Error{file, ErrNotFound}
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitList(path string) []string {
|
|
||||||
// The same implementation is used in SplitList in path/filepath;
|
|
||||||
// consider changing path/filepath when changing this.
|
|
||||||
|
|
||||||
if path == "" {
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split path, respecting but preserving quotes.
|
|
||||||
list := []string{}
|
|
||||||
start := 0
|
|
||||||
quo := false
|
|
||||||
for i := 0; i < len(path); i++ {
|
|
||||||
switch c := path[i]; {
|
|
||||||
case c == '"':
|
|
||||||
quo = !quo
|
|
||||||
case c == os.PathListSeparator && !quo:
|
|
||||||
list = append(list, path[start:i])
|
|
||||||
start = i + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
list = append(list, path[start:])
|
|
||||||
|
|
||||||
// Remove quotes.
|
|
||||||
for i, s := range list {
|
|
||||||
if strings.Contains(s, `"`) {
|
|
||||||
list[i] = strings.Replace(s, `"`, "", -1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ func createEnv(dir, PATH, PATHEXT string) []string {
|
||||||
env := os.Environ()
|
env := os.Environ()
|
||||||
env = updateEnv(env, "PATHEXT", PATHEXT)
|
env = updateEnv(env, "PATHEXT", PATHEXT)
|
||||||
// Add dir in front of every directory in the PATH.
|
// Add dir in front of every directory in the PATH.
|
||||||
dirs := splitList(PATH)
|
dirs := filepath.SplitList(PATH)
|
||||||
for i := range dirs {
|
for i := range dirs {
|
||||||
dirs[i] = filepath.Join(dir, dirs[i])
|
dirs[i] = filepath.Join(dir, dirs[i])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue