fzf/src/util/util_unix.go

54 lines
1.2 KiB
Go
Raw Normal View History

2022-03-29 12:20:33 +00:00
//go:build !windows
package util
import (
"os"
"os/exec"
2017-01-07 16:30:31 +00:00
"syscall"
2023-02-14 14:21:34 +00:00
"golang.org/x/sys/unix"
)
// ExecCommand executes the given command with $SHELL
func ExecCommand(command string, setpgid bool) *exec.Cmd {
shell := os.Getenv("SHELL")
if len(shell) == 0 {
shell = "sh"
}
return ExecCommandWith(shell, command, setpgid)
}
// ExecCommandWith executes the given command with the specified shell
func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {
cmd := exec.Command(shell, "-c", command)
if setpgid {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
return cmd
}
// KillCommand kills the process for the given command
func KillCommand(cmd *exec.Cmd) error {
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
// IsWindows returns true on Windows
func IsWindows() bool {
return false
}
2017-01-07 16:30:31 +00:00
// SetNonblock executes syscall.SetNonblock on file descriptor
2017-01-07 16:30:31 +00:00
func SetNonblock(file *os.File, nonblock bool) {
syscall.SetNonblock(int(file.Fd()), nonblock)
}
// Read executes syscall.Read on file descriptor
func Read(fd int, b []byte) (int, error) {
return syscall.Read(int(fd), b)
}
2023-02-14 14:21:34 +00:00
func SetStdin(file *os.File) {
unix.Dup2(int(file.Fd()), 0)
}