Determine shell type once by the basename

pull/3189/merge
Junegunn Choi 1 month ago
parent 159a37fa37
commit 7ed6c7905c
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -6,14 +6,24 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
) )
type shellType int
const (
shellTypeUnknown shellType = iota
shellTypeCmd
shellTypePowerShell
)
type Executor struct { type Executor struct {
shell string shell string
shellType shellType
args []string args []string
shellPath atomic.Value shellPath atomic.Value
} }
@ -27,16 +37,20 @@ func NewExecutor(withShell string) *Executor {
shell = "cmd" shell = "cmd"
} }
shellType := shellTypeUnknown
basename := filepath.Base(shell)
if len(args) > 0 { if len(args) > 0 {
args = args[1:] args = args[1:]
} else if strings.Contains(shell, "cmd") { } else if strings.HasPrefix(basename, "cmd") {
shellType = shellTypeCmd
args = []string{"/v:on/s/c"} args = []string{"/v:on/s/c"}
} else if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") { } else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") {
shellType = shellTypePowerShell
args = []string{"-NoProfile", "-Command"} args = []string{"-NoProfile", "-Command"}
} else { } else {
args = []string{"-c"} args = []string{"-c"}
} }
return &Executor{shell: shell, args: args} return &Executor{shell: shell, shellType: shellType, args: args}
} }
// ExecCommand executes the given command with $SHELL // ExecCommand executes the given command with $SHELL
@ -58,7 +72,7 @@ func (x *Executor) ExecCommand(command string, setpgid bool) *exec.Cmd {
x.shellPath.Store(shell) x.shellPath.Store(shell)
} }
var cmd *exec.Cmd var cmd *exec.Cmd
if strings.Contains(shell, "cmd") { if x.shellType == shellTypeCmd {
cmd = exec.Command(shell) cmd = exec.Command(shell)
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: false, HideWindow: false,
@ -96,7 +110,8 @@ func (x *Executor) Become(stdin *os.File, environ []string, command string) {
} }
func (x *Executor) QuoteEntry(entry string) string { func (x *Executor) QuoteEntry(entry string) string {
if strings.Contains(x.shell, "cmd") { switch x.shellType {
case shellTypeCmd:
// backslash escaping is done here for applications // backslash escaping is done here for applications
// (see ripgrep test case in terminal_test.go#TestWindowsCommands) // (see ripgrep test case in terminal_test.go#TestWindowsCommands)
escaped := strings.Replace(entry, `\`, `\\`, -1) escaped := strings.Replace(entry, `\`, `\\`, -1)
@ -106,10 +121,10 @@ func (x *Executor) QuoteEntry(entry string) string {
return r.ReplaceAllStringFunc(escaped, func(match string) string { return r.ReplaceAllStringFunc(escaped, func(match string) string {
return "^" + match return "^" + match
}) })
} else if strings.Contains(x.shell, "pwsh") || strings.Contains(x.shell, "powershell") { case shellTypePowerShell:
escaped := strings.Replace(entry, `"`, `\"`, -1) escaped := strings.Replace(entry, `"`, `\"`, -1)
return "'" + strings.Replace(escaped, "'", "''", -1) + "'" return "'" + strings.Replace(escaped, "'", "''", -1) + "'"
} else { default:
return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'" return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"
} }
} }

Loading…
Cancel
Save