github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/helper/discover/discover.go (about)

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