shell/linux/utils.go

76 lines
1.4 KiB
Go
Raw Normal View History

2024-07-04 22:40:13 +02:00
package linux
import (
"errors"
"fmt"
2024-08-25 00:51:29 +02:00
cmd2 "git.eggactyl.cloud/Eggactyl/shell/cmd"
2024-07-04 23:14:10 +02:00
"golang.org/x/sys/unix"
"io/fs"
"os"
2024-07-04 22:40:13 +02:00
"os/exec"
)
2024-07-04 23:14:10 +02:00
func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) {
2024-07-10 00:53:22 +02:00
whichOut, err := cmd2.Which(command, cmd2.BasicOptions{
2024-07-10 00:44:28 +02:00
Env: cmd.Options.Env,
Sources: cmd.Options.Sources,
Cwd: cmd.Options.Cwd,
})
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 {
return false, nil
2024-07-04 23:14:10 +02:00
}
2024-07-11 21:54:59 +02:00
if err := unix.Access(whichOut, unix.X_OK); err != nil {
2024-07-04 23:27:41 +02:00
if err == unix.EACCES {
2024-07-04 23:14:10 +02:00
return false, nil
} else {
2024-07-04 23:27:41 +02:00
fmt.Println(err)
2024-07-04 23:14:10 +02:00
return false, err
}
}
return true, nil
}
2024-07-04 22:40:13 +02:00
func (cmd *LinuxCommand) doesCommandExist(command string) (bool, error) {
shellCmd := exec.Command(cmd.Options.Shell, "-c", fmt.Sprintf("command -v %s", command))
if err := shellCmd.Start(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 1 {
return false, nil
} else {
return false, ErrRunningCmd
}
}
}
if err := shellCmd.Wait(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 1 {
return false, nil
} else {
return false, ErrRunningCmd
}
}
}
return true, nil
}