github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/which/which.go (about)

     1  package which
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/lmorg/murex/utils/consts"
     7  )
     8  
     9  // Which works similarly to the UNIX command with the same name
    10  func Which(cmd string) string {
    11  	envPath := os.Getenv("PATH")
    12  
    13  	for _, path := range SplitPath(envPath) {
    14  		filepath := path + consts.PathSlash + cmd
    15  		_, err := os.Stat(filepath)
    16  		if !os.IsNotExist(err) {
    17  			return filepath
    18  		}
    19  	}
    20  
    21  	return ""
    22  }