2015-09-20 01:47:04 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-11-03 21:26:09 +00:00
|
|
|
"os/exec"
|
2015-09-20 01:47:04 +00:00
|
|
|
"path/filepath"
|
2015-11-03 04:28:37 +00:00
|
|
|
"runtime"
|
2017-10-18 22:35:31 +00:00
|
|
|
"strings"
|
2015-09-20 01:47:04 +00:00
|
|
|
)
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// NomadExecutable checks the current executable, then $GOPATH/bin, and finally
|
|
|
|
// the CWD, in that order. If it can't be found, an error is returned.
|
2015-09-20 01:47:04 +00:00
|
|
|
func NomadExecutable() (string, error) {
|
2015-11-03 04:28:37 +00:00
|
|
|
nomadExe := "nomad"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
nomadExe = "nomad.exe"
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
// Check the current executable.
|
2017-03-14 19:56:31 +00:00
|
|
|
bin, err := os.Executable()
|
2015-09-20 01:47:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to determine the nomad executable: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-14 23:04:33 +00:00
|
|
|
if _, err := os.Stat(bin); err == nil && isNomad(bin, nomadExe) {
|
2015-09-20 01:47:04 +00:00
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 21:26:09 +00:00
|
|
|
// Check the $PATH
|
|
|
|
if bin, err := exec.LookPath(nomadExe); err == nil {
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
// Check the $GOPATH.
|
|
|
|
bin = filepath.Join(os.Getenv("GOPATH"), "bin", nomadExe)
|
|
|
|
if _, err := os.Stat(bin); err == nil {
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the CWD.
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2015-10-07 10:18:19 +00:00
|
|
|
return "", fmt.Errorf("Could not find Nomad executable (%v): %v", nomadExe, err)
|
2015-09-20 01:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bin = filepath.Join(pwd, nomadExe)
|
|
|
|
if _, err := os.Stat(bin); err == nil {
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
2015-09-30 21:30:09 +00:00
|
|
|
// Check CWD/bin
|
|
|
|
bin = filepath.Join(pwd, "bin", nomadExe)
|
|
|
|
if _, err := os.Stat(bin); err == nil {
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
return "", fmt.Errorf("Could not find Nomad executable (%v)", nomadExe)
|
|
|
|
}
|
2017-10-18 22:35:31 +00:00
|
|
|
|
|
|
|
func isNomad(path, nomadExe string) bool {
|
|
|
|
if strings.HasSuffix(path, ".test") || strings.HasSuffix(path, ".test.exe") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|