|
|
|
@ -3,58 +3,52 @@
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"syscall"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var shellPath atomic.Value
|
|
|
|
|
type Executor struct {
|
|
|
|
|
shell string
|
|
|
|
|
args []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecCommand executes the given command with $SHELL
|
|
|
|
|
func ExecCommand(command string, setpgid bool) *exec.Cmd {
|
|
|
|
|
var shell string
|
|
|
|
|
if cached := shellPath.Load(); cached != nil {
|
|
|
|
|
shell = cached.(string)
|
|
|
|
|
} else {
|
|
|
|
|
shell = os.Getenv("SHELL")
|
|
|
|
|
if len(shell) == 0 {
|
|
|
|
|
shell = "cmd"
|
|
|
|
|
} else if strings.Contains(shell, "/") {
|
|
|
|
|
out, err := exec.Command("cygpath", "-w", shell).Output()
|
|
|
|
|
if err == nil {
|
|
|
|
|
shell = strings.Trim(string(out), "\n")
|
|
|
|
|
}
|
|
|
|
|
func NewExecutor(withShell string) *Executor {
|
|
|
|
|
shell := os.Getenv("SHELL")
|
|
|
|
|
args := strings.Fields(withShell)
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
shell = args[0]
|
|
|
|
|
} else if len(shell) == 0 {
|
|
|
|
|
shell = "cmd"
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(shell, "/") {
|
|
|
|
|
out, err := exec.Command("cygpath", "-w", shell).Output()
|
|
|
|
|
if err == nil {
|
|
|
|
|
shell = strings.Trim(string(out), "\n")
|
|
|
|
|
}
|
|
|
|
|
shellPath.Store(shell)
|
|
|
|
|
}
|
|
|
|
|
return ExecCommandWith(shell, command, setpgid)
|
|
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
args = args[1:]
|
|
|
|
|
} else if strings.Contains(shell, "cmd") {
|
|
|
|
|
args = []string{"/v:on/s/c"}
|
|
|
|
|
} else if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {
|
|
|
|
|
args = []string{"-NoProfile", "-Command"}
|
|
|
|
|
} else {
|
|
|
|
|
args = []string{"-c"}
|
|
|
|
|
}
|
|
|
|
|
return &Executor{shell, args}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecCommandWith executes the given command with the specified shell
|
|
|
|
|
// ExecCommand executes the given command with $SHELL
|
|
|
|
|
// FIXME: setpgid is unused. We set it in the Unix implementation so that we
|
|
|
|
|
// can kill preview process with its child processes at once.
|
|
|
|
|
// NOTE: For "powershell", we should ideally set output encoding to UTF8,
|
|
|
|
|
// but it is left as is now because no adverse effect has been observed.
|
|
|
|
|
func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
if strings.Contains(shell, "cmd") {
|
|
|
|
|
cmd = exec.Command(shell)
|
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
|
HideWindow: false,
|
|
|
|
|
CmdLine: fmt.Sprintf(` /v:on/s/c "%s"`, command),
|
|
|
|
|
CreationFlags: 0,
|
|
|
|
|
}
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {
|
|
|
|
|
cmd = exec.Command(shell, "-NoProfile", "-Command", command)
|
|
|
|
|
} else {
|
|
|
|
|
cmd = exec.Command(shell, "-c", command)
|
|
|
|
|
}
|
|
|
|
|
func (x *Executor) ExecCommand(command string, setpgid bool) *exec.Cmd {
|
|
|
|
|
cmd := exec.Command(x.shell, append(x.args, command)...)
|
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
|
HideWindow: false,
|
|
|
|
|
CreationFlags: 0,
|
|
|
|
@ -62,6 +56,25 @@ func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x *Executor) QuoteEntry(entry string) string {
|
|
|
|
|
if strings.Contains(x.shell, "cmd") {
|
|
|
|
|
// backslash escaping is done here for applications
|
|
|
|
|
// (see ripgrep test case in terminal_test.go#TestWindowsCommands)
|
|
|
|
|
escaped := strings.Replace(entry, `\`, `\\`, -1)
|
|
|
|
|
escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`
|
|
|
|
|
// caret is the escape character for cmd shell
|
|
|
|
|
r, _ := regexp.Compile(`[&|<>()@^%!"]`)
|
|
|
|
|
return r.ReplaceAllStringFunc(escaped, func(match string) string {
|
|
|
|
|
return "^" + match
|
|
|
|
|
})
|
|
|
|
|
} else if strings.Contains(x.shell, "pwsh") || strings.Contains(x.shell, "powershell") {
|
|
|
|
|
escaped := strings.Replace(entry, `"`, `\"`, -1)
|
|
|
|
|
return "'" + strings.Replace(escaped, "'", "''", -1) + "'"
|
|
|
|
|
} else {
|
|
|
|
|
return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// KillCommand kills the process for the given command
|
|
|
|
|
func KillCommand(cmd *exec.Cmd) error {
|
|
|
|
|
return cmd.Process.Kill()
|
|
|
|
|