2022-03-29 12:20:33 +00:00
|
|
|
//go:build !windows
|
2016-10-24 00:44:56 +00:00
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-04-27 09:36:37 +00:00
|
|
|
"fmt"
|
2016-10-24 00:44:56 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-04-27 09:36:37 +00:00
|
|
|
"strings"
|
2017-01-07 16:30:31 +00:00
|
|
|
"syscall"
|
2023-02-14 14:21:34 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-10-24 00:44:56 +00:00
|
|
|
)
|
|
|
|
|
2024-04-27 09:36:37 +00:00
|
|
|
type Executor struct {
|
|
|
|
shell string
|
|
|
|
args []string
|
|
|
|
escaper *strings.Replacer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutor(withShell string) *Executor {
|
2016-10-24 00:44:56 +00:00
|
|
|
shell := os.Getenv("SHELL")
|
2024-04-27 09:36:37 +00:00
|
|
|
args := strings.Fields(withShell)
|
|
|
|
if len(args) > 0 {
|
|
|
|
shell = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
} else {
|
|
|
|
if len(shell) == 0 {
|
|
|
|
shell = "sh"
|
|
|
|
}
|
|
|
|
args = []string{"-c"}
|
2016-10-24 00:44:56 +00:00
|
|
|
}
|
2024-04-27 09:36:37 +00:00
|
|
|
|
|
|
|
var escaper *strings.Replacer
|
|
|
|
tokens := strings.Split(shell, "/")
|
|
|
|
if tokens[len(tokens)-1] == "fish" {
|
|
|
|
// https://fishshell.com/docs/current/language.html#quotes
|
|
|
|
// > The only meaningful escape sequences in single quotes are \', which
|
|
|
|
// > escapes a single quote and \\, which escapes the backslash symbol.
|
|
|
|
escaper = strings.NewReplacer("\\", "\\\\", "'", "\\'")
|
|
|
|
} else {
|
|
|
|
escaper = strings.NewReplacer("'", "'\\''")
|
|
|
|
}
|
|
|
|
return &Executor{shell, args, escaper}
|
2017-09-28 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 09:36:37 +00:00
|
|
|
// ExecCommand executes the given command with $SHELL
|
|
|
|
func (x *Executor) ExecCommand(command string, setpgid bool) *exec.Cmd {
|
|
|
|
cmd := exec.Command(x.shell, append(x.args, command)...)
|
2018-09-27 06:27:08 +00:00
|
|
|
if setpgid {
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-04-27 09:36:37 +00:00
|
|
|
func (x *Executor) QuoteEntry(entry string) string {
|
|
|
|
return "'" + x.escaper.Replace(entry) + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Executor) Become(stdin *os.File, environ []string, command string) {
|
|
|
|
shellPath, err := exec.LookPath(x.shell)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "fzf (become): %s\n", err.Error())
|
|
|
|
Exit(127)
|
|
|
|
}
|
|
|
|
args := append([]string{shellPath}, append(x.args, command)...)
|
|
|
|
SetStdin(stdin)
|
|
|
|
syscall.Exec(shellPath, args, environ)
|
|
|
|
}
|
|
|
|
|
2018-09-27 06:27:08 +00:00
|
|
|
// KillCommand kills the process for the given command
|
|
|
|
func KillCommand(cmd *exec.Cmd) error {
|
|
|
|
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
2016-10-24 00:44:56 +00:00
|
|
|
}
|
2016-11-06 17:15:34 +00:00
|
|
|
|
|
|
|
// IsWindows returns true on Windows
|
|
|
|
func IsWindows() bool {
|
|
|
|
return false
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
|
2018-09-27 06:27:08 +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)
|
|
|
|
}
|
2017-05-24 16:36:59 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|