2015-09-20 01:47:04 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/kardianos/osext"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
nomadExe = "nomad"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Checks the current executable, then $GOPATH/bin, and finally the CWD, in that
|
|
|
|
// order. If it can't be found, an error is returned.
|
|
|
|
func NomadExecutable() (string, error) {
|
|
|
|
// Check the current executable.
|
|
|
|
bin, err := osext.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to determine the nomad executable: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Base(bin) == nomadExe {
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|