github.com/tschmi5/nomad@v0.11.8/helper/discover/discover.go (about)

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