github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbnm/findbin.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  
     9  	"github.com/keybase/client/go/utils"
    10  )
    11  
    12  var errKeybaseNotFound = errors.New("failed to find the keybase binary")
    13  
    14  // findKeybaseBinary returns the path to a Keybase binary, if it finds it.
    15  func findKeybaseBinary(name string) (string, error) {
    16  	// Is it near the kbnm binary?
    17  	binPath, err := utils.BinPath()
    18  	if err == nil {
    19  		path := filepath.Join(filepath.Dir(binPath), name)
    20  		if _, err := os.Stat(path); !os.IsNotExist(err) {
    21  			return path, nil
    22  		}
    23  	}
    24  
    25  	// Is it in our PATH?
    26  	path, err := exec.LookPath(name)
    27  	if err == nil {
    28  		return path, nil
    29  	}
    30  
    31  	// Last ditch effort!
    32  	path = guessKeybasePath(name)
    33  	if _, err := os.Stat(path); !os.IsNotExist(err) {
    34  		return path, nil
    35  	}
    36  
    37  	return "", errKeybaseNotFound
    38  }