fixed permission checking on commands, added "which" command.

This commit is contained in:
Shane C 2024-07-04 20:37:59 -04:00
parent d127424b9f
commit 2cc3517f37
Signed by: shane
GPG key ID: E46B5FEA35B22FF9
2 changed files with 44 additions and 2 deletions

30
cmd/which.go Normal file
View file

@ -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
}

View file

@ -3,6 +3,7 @@ package linux
import ( import (
"errors" "errors"
"fmt" "fmt"
cmd2 "git.shadowhosting.xyz/Eggactyl/shell/cmd"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"io/fs" "io/fs"
"os" "os"
@ -11,9 +12,20 @@ import (
func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) {
whichOut, err := cmd2.Which(command)
if err != nil {
if errors.Is(err, cmd2.ErrNotFound) {
if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) { if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) {
return false, err return false, err
} }
} else {
return false, err
}
}
if len(whichOut) > 0 {
command = whichOut
}
if err := unix.Access(command, unix.X_OK); err != nil { if err := unix.Access(command, unix.X_OK); err != nil {
if err == unix.EACCES { if err == unix.EACCES {