From 2cc3517f37cc901954329e85202cf3ab9311e3b5 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 20:37:59 -0400 Subject: [PATCH] fixed permission checking on commands, added "which" command. --- cmd/which.go | 30 ++++++++++++++++++++++++++++++ linux/utils.go | 16 ++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 cmd/which.go diff --git a/cmd/which.go b/cmd/which.go new file mode 100644 index 0000000..c7acd6a --- /dev/null +++ b/cmd/which.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "errors" + "fmt" + "os/exec" + "strings" +) + +var ErrNotFound = errors.New("which: command not found") + +func Which(cmd string) (string, error) { + + command := exec.Command("which", cmd) + + outputBytes, err := command.Output() + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + if exitErr.ExitCode() == 1 { + return "", ErrNotFound + } else { + return "", fmt.Errorf("command error: %w", err) + } + } + } + + return strings.Trim(string(outputBytes), "\n"), nil + +} diff --git a/linux/utils.go b/linux/utils.go index ba6a1d5..7146b29 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -3,6 +3,7 @@ package linux import ( "errors" "fmt" + cmd2 "git.shadowhosting.xyz/Eggactyl/shell/cmd" "golang.org/x/sys/unix" "io/fs" "os" @@ -11,8 +12,19 @@ import ( func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { - if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) { - return false, err + whichOut, err := cmd2.Which(command) + if err != nil { + if errors.Is(err, cmd2.ErrNotFound) { + if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) { + return false, err + } + } else { + return false, err + } + } + + if len(whichOut) > 0 { + command = whichOut } if err := unix.Access(command, unix.X_OK); err != nil {