1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +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 := ""
// Windows does not seem to like quotes around the command
if c.Platform.OS == "windows" {
quotedCommand = commandStr
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)
quotedCommand = strings.NewReplacer(
"^", "^^",
"&", "^&",
"|", "^|",
"<", "^<",
">", "^>",
"%", "^%",
).Replace(commandStr)
} else {
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
func (c *OSCommand) Quote(message string) string {
if c.Platform.OS == "windows" {
message = strings.Replace(message, `"`, `"'"'"`, -1)
message = strings.Replace(message, `\"`, `\\"`, -1)
message = strings.NewReplacer(
`"`, `"'"'"`,
`\"`, `\\"`,
).Replace(message)
} else {
message = strings.Replace(message, `\`, `\\`, -1)
message = strings.Replace(message, `"`, `\"`, -1)
message = strings.Replace(message, "`", "\\`", -1)
message = strings.Replace(message, "$", "\\$", -1)
message = strings.NewReplacer(
`\`, `\\`,
`"`, `\"`,
`$`, `\$`,
"`", "\\`",
).Replace(message)
}
escapedQuote := c.Platform.EscapedQuote
return escapedQuote + message + escapedQuote