fixed permission checking on commands, added "which" command.
This commit is contained in:
parent
d127424b9f
commit
2cc3517f37
2 changed files with 44 additions and 2 deletions
30
cmd/which.go
Normal file
30
cmd/which.go
Normal 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
|
||||||
|
|
||||||
|
}
|
|
@ -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,8 +12,19 @@ import (
|
||||||
|
|
||||||
func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) {
|
func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) {
|
||||||
|
|
||||||
if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) {
|
whichOut, err := cmd2.Which(command)
|
||||||
return false, err
|
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 {
|
if err := unix.Access(command, unix.X_OK); err != nil {
|
||||||
|
|
Loading…
Reference in a new issue