github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/helper/discover/discover.go (about) 1 package discover 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "runtime" 9 10 "github.com/kardianos/osext" 11 ) 12 13 // Checks the current executable, then $GOPATH/bin, and finally the CWD, in that 14 // order. If it can't be found, an error is returned. 15 func NomadExecutable() (string, error) { 16 nomadExe := "nomad" 17 if runtime.GOOS == "windows" { 18 nomadExe = "nomad.exe" 19 } 20 21 // Check the current executable. 22 bin, err := osext.Executable() 23 if err != nil { 24 return "", fmt.Errorf("Failed to determine the nomad executable: %v", err) 25 } 26 27 if filepath.Base(bin) == nomadExe { 28 return bin, nil 29 } 30 31 // Check the $PATH 32 if bin, err := exec.LookPath(nomadExe); err == nil { 33 return bin, nil 34 } 35 36 // Check the $GOPATH. 37 bin = filepath.Join(os.Getenv("GOPATH"), "bin", nomadExe) 38 if _, err := os.Stat(bin); err == nil { 39 return bin, nil 40 } 41 42 // Check the CWD. 43 pwd, err := os.Getwd() 44 if err != nil { 45 return "", fmt.Errorf("Could not find Nomad executable (%v): %v", nomadExe, err) 46 } 47 48 bin = filepath.Join(pwd, nomadExe) 49 if _, err := os.Stat(bin); err == nil { 50 return bin, nil 51 } 52 53 // Check CWD/bin 54 bin = filepath.Join(pwd, "bin", nomadExe) 55 if _, err := os.Stat(bin); err == nil { 56 return bin, nil 57 } 58 59 return "", fmt.Errorf("Could not find Nomad executable (%v)", nomadExe) 60 }