mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-03-19 21:28:28 +02:00
By constructing an arg vector manually, we no longer need to quote arguments Mandate that args must be passed when building a command Now you need to provide an args array when building a command. There are a handful of places where we need to deal with a string, such as with user-defined custom commands, and for those we now require that at the callsite they use str.ToArgv to do that. I don't want to provide a method out of the box for it because I want to discourage its use. For some reason we were invoking a command through a shell when amending a commit, and I don't believe we needed to do that as there was nothing user- supplied about the command. So I've switched to using a regular command out- side the shell there
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package oscommands
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
|
"github.com/mgutz/str"
|
|
)
|
|
|
|
type ICmdObjBuilder interface {
|
|
// NewFromArgs takes a slice of strings like []string{"git", "commit"} and returns a new command object.
|
|
New(args []string) ICmdObj
|
|
// NewShell takes a string like `git commit` and returns an executable shell command for it e.g. `sh -c 'git commit'`
|
|
NewShell(commandStr string) ICmdObj
|
|
// Quote wraps a string in quotes with any necessary escaping applied. The reason for bundling this up with the other methods in this interface is that we basically always need to make use of this when creating new command objects.
|
|
Quote(str string) string
|
|
}
|
|
|
|
type CmdObjBuilder struct {
|
|
runner ICmdObjRunner
|
|
platform *Platform
|
|
}
|
|
|
|
// poor man's version of explicitly saying that struct X implements interface Y
|
|
var _ ICmdObjBuilder = &CmdObjBuilder{}
|
|
|
|
func (self *CmdObjBuilder) New(args []string) ICmdObj {
|
|
cmd := secureexec.Command(args[0], args[1:]...)
|
|
cmd.Env = os.Environ()
|
|
|
|
return &CmdObj{
|
|
args: args,
|
|
cmd: cmd,
|
|
runner: self.runner,
|
|
}
|
|
}
|
|
|
|
func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj {
|
|
var quotedCommand string
|
|
// Windows does not seem to like quotes around the command
|
|
if self.platform.OS == "windows" {
|
|
quotedCommand = strings.NewReplacer(
|
|
"^", "^^",
|
|
"&", "^&",
|
|
"|", "^|",
|
|
"<", "^<",
|
|
">", "^>",
|
|
"%", "^%",
|
|
).Replace(commandStr)
|
|
} else {
|
|
quotedCommand = self.Quote(commandStr)
|
|
}
|
|
|
|
cmdArgs := str.ToArgv(fmt.Sprintf("%s %s %s", self.platform.Shell, self.platform.ShellArg, quotedCommand))
|
|
|
|
return self.New(cmdArgs)
|
|
}
|
|
|
|
func (self *CmdObjBuilder) CloneWithNewRunner(decorate func(ICmdObjRunner) ICmdObjRunner) *CmdObjBuilder {
|
|
decoratedRunner := decorate(self.runner)
|
|
|
|
return &CmdObjBuilder{
|
|
runner: decoratedRunner,
|
|
platform: self.platform,
|
|
}
|
|
}
|
|
|
|
const CHARS_REQUIRING_QUOTES = "\"\\$` "
|
|
|
|
// If you update this method, be sure to update CHARS_REQUIRING_QUOTES
|
|
func (self *CmdObjBuilder) Quote(message string) string {
|
|
var quote string
|
|
if self.platform.OS == "windows" {
|
|
quote = `\"`
|
|
message = strings.NewReplacer(
|
|
`"`, `"'"'"`,
|
|
`\"`, `\\"`,
|
|
).Replace(message)
|
|
} else {
|
|
quote = `"`
|
|
message = strings.NewReplacer(
|
|
`\`, `\\`,
|
|
`"`, `\"`,
|
|
`$`, `\$`,
|
|
"`", "\\`",
|
|
).Replace(message)
|
|
}
|
|
return quote + message + quote
|
|
}
|