1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-21 22:43:27 +02:00

Use replacer

This commit is contained in:
Ryooooooga 2021-10-12 22:18:17 +09:00 committed by Jesse Duffield
parent 92f03a7872
commit 4171b7613c

View File

@ -202,13 +202,14 @@ func (c *OSCommand) ShellCommandFromString(commandStr string) *exec.Cmd {
quotedCommand := "" quotedCommand := ""
// Windows does not seem to like quotes around the command // Windows does not seem to like quotes around the command
if c.Platform.OS == "windows" { if c.Platform.OS == "windows" {
quotedCommand = commandStr quotedCommand = strings.NewReplacer(
quotedCommand = strings.Replace(quotedCommand, "^", "^^", -1) "^", "^^",
quotedCommand = strings.Replace(quotedCommand, "&", "^&", -1) "&", "^&",
quotedCommand = strings.Replace(quotedCommand, "|", "^|", -1) "|", "^|",
quotedCommand = strings.Replace(quotedCommand, "<", "^<", -1) "<", "^<",
quotedCommand = strings.Replace(quotedCommand, ">", "^>", -1) ">", "^>",
quotedCommand = strings.Replace(quotedCommand, "%", "^%", -1) "%", "^%",
).Replace(commandStr)
} else { } else {
quotedCommand = c.Quote(commandStr) quotedCommand = c.Quote(commandStr)
} }
@ -334,13 +335,17 @@ func (c *OSCommand) PrepareShellSubProcess(command string) *exec.Cmd {
// Quote wraps a message in platform-specific quotation marks // Quote wraps a message in platform-specific quotation marks
func (c *OSCommand) Quote(message string) string { func (c *OSCommand) Quote(message string) string {
if c.Platform.OS == "windows" { if c.Platform.OS == "windows" {
message = strings.Replace(message, `"`, `"'"'"`, -1) message = strings.NewReplacer(
message = strings.Replace(message, `\"`, `\\"`, -1) `"`, `"'"'"`,
`\"`, `\\"`,
).Replace(message)
} else { } else {
message = strings.Replace(message, `\`, `\\`, -1) message = strings.NewReplacer(
message = strings.Replace(message, `"`, `\"`, -1) `\`, `\\`,
message = strings.Replace(message, "`", "\\`", -1) `"`, `\"`,
message = strings.Replace(message, "$", "\\$", -1) `$`, `\$`,
"`", "\\`",
).Replace(message)
} }
escapedQuote := c.Platform.EscapedQuote escapedQuote := c.Platform.EscapedQuote
return escapedQuote + message + escapedQuote return escapedQuote + message + escapedQuote