mukan-ignite/ignite/pkg/xexec/xexec.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

68 lines
1.7 KiB
Go

package xexec
import (
"os"
"os/exec"
"path/filepath"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/goenv"
)
// IsExec checks if a file is executable by anyone.
func IsExec(binaryPath string) (bool, error) {
info, err := os.Stat(binaryPath)
if err != nil {
return false, err
}
if m := info.Mode(); !m.IsDir() && m&0o111 != 0 {
return true, nil
}
return false, nil
}
// ResolveAbsPath searches for an executable file in the current
// working directory, the directories defined by the PATH environment
// variable and in the Go binary path. Once found returns the absolute
// path to the file.
func ResolveAbsPath(filePath string) (path string, err error) {
// Check if file exists and it's an executable file
if path, err = filepath.Abs(filePath); err == nil {
if ok, _ := IsExec(path); ok {
return path, nil
}
}
// Search file in the directories defined by the PATH env variable
path, err = exec.LookPath(filePath)
if err == nil {
return path, nil
}
// When PATH search fails check if file is located in the Go binary path
path = filepath.Join(goenv.Bin(), filePath)
if ok, _ := IsExec(path); ok {
return path, nil
}
return path, err
}
// TryResolveAbsPath searches for an executable file in the current
// working directory, the directories defined by the PATH environment
// variable and in the Go binary path. Once found returns the absolute
// path to the file, or otherwise it returns the file path unmodified.
func TryResolveAbsPath(filePath string) string {
if path, err := ResolveAbsPath(filePath); err == nil {
return path
}
return filePath
}
// IsCommandAvailable checks if command is available on user's path.
func IsCommandAvailable(name string) bool {
_, err := ResolveAbsPath(name)
return err == nil
}