shell/linux/utils.go

58 lines
1 KiB
Go

package linux
import (
"errors"
"fmt"
"golang.org/x/sys/unix"
"io/fs"
"os"
"os/exec"
)
func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) {
if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) {
return false, err
}
if err := unix.Access(command, unix.X_OK); err != nil {
if !errors.Is(err, unix.EACCES) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
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
}