diff options
Diffstat (limited to 'client/go/util')
-rw-r--r-- | client/go/util/execvp.go | 45 | ||||
-rw-r--r-- | client/go/util/execvp_windows.go | 22 | ||||
-rw-r--r-- | client/go/util/io.go | 9 |
3 files changed, 76 insertions, 0 deletions
diff --git a/client/go/util/execvp.go b/client/go/util/execvp.go new file mode 100644 index 00000000000..2b47af4de1b --- /dev/null +++ b/client/go/util/execvp.go @@ -0,0 +1,45 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Author: arnej + +//go:build !windows + +package util + +import ( + "fmt" + "os" + "strings" + + "github.com/vespa-engine/vespa/client/go/trace" + "golang.org/x/sys/unix" +) + +func findInPath(prog string) string { + if strings.Contains(prog, "/") { + return prog + } + path := strings.Split(os.Getenv("PATH"), ":") + for _, dir := range path { + fn := dir + "/" + prog + if IsExecutableFile(fn) { + return fn + } + } + return prog +} + +func Execvp(prog string, argv []string) error { + return Execvpe(prog, argv, os.Environ()) +} + +func Execvpe(prog string, argv []string, envv []string) error { + prog = findInPath(prog) + argv[0] = prog + return Execve(prog, argv, envv) +} + +func Execve(prog string, argv []string, envv []string) error { + trace.Trace("run cmd:", strings.Join(argv, " ")) + err := unix.Exec(prog, argv, envv) + return fmt.Errorf("cannot execute '%s': %v", prog, err) +} diff --git a/client/go/util/execvp_windows.go b/client/go/util/execvp_windows.go new file mode 100644 index 00000000000..24b67e221c8 --- /dev/null +++ b/client/go/util/execvp_windows.go @@ -0,0 +1,22 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Author: arnej + +//go:build windows + +package util + +import ( + "fmt" +) + +func Execvp(prog string, argv []string) error { + return fmt.Errorf("cannot execvp on windows: %s", prog) +} + +func Execvpe(prog string, argv []string, envv []string) error { + return fmt.Errorf("cannot execvp on windows: %s", prog) +} + +func Execve(prog string, argv []string, envv []string) error { + return fmt.Errorf("cannot execvp on windows: %s", prog) +} diff --git a/client/go/util/io.go b/client/go/util/io.go index 6aab64b8827..50122324fae 100644 --- a/client/go/util/io.go +++ b/client/go/util/io.go @@ -32,6 +32,15 @@ func IsRegularFile(path string) bool { return !errors.Is(err, os.ErrNotExist) && info != nil && info.Mode().IsRegular() } +// Returns true if the given path points to an executable +func IsExecutableFile(path string) bool { + info, err := os.Stat(path) + return !errors.Is(err, os.ErrNotExist) && + info != nil && + info.Mode().IsRegular() && + ((info.Mode() & 0111) == 0111) +} + // Returns the content of a reader as a string func ReaderToString(reader io.Reader) string { var buffer strings.Builder |