1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

Switched back to github.com/mgutz/str instaid of a copy of ToArgv

This commit is contained in:
mjarkk
2018-11-10 08:57:02 +01:00
parent 500267417b
commit 834e42897d
12 changed files with 2272 additions and 116 deletions

View File

@ -11,6 +11,7 @@ import (
"unicode/utf8"
"github.com/kr/pty"
"github.com/mgutz/str"
)
// RunCommandWithOutputLiveWrapper runs a command and return every word that gets written in stdout
@ -21,7 +22,7 @@ import (
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) (errorMessage string, codeError error) {
cmdOutput := []string{}
splitCmd := ToArgv(command)
splitCmd := str.ToArgv(command)
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.Env = os.Environ()

View File

@ -9,6 +9,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mgutz/str"
"github.com/sirupsen/logrus"
gitconfig "github.com/tcnksm/go-gitconfig"
)
@ -49,7 +50,7 @@ func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand {
// RunCommandWithOutput wrapper around commands returning their output and error
func (c *OSCommand) RunCommandWithOutput(command string) (string, error) {
c.Log.WithField("command", command).Info("RunCommand")
splitCmd := ToArgv(command)
splitCmd := str.ToArgv(command)
c.Log.Info(splitCmd)
return sanitisedCommandOutput(
c.command(splitCmd[0], splitCmd[1:]...).CombinedOutput(),

View File

@ -1,114 +0,0 @@
// ToArgv is copied from github.com/mgutz/str
package commands
import "runtime"
// ToArgv converts string s into an argv for exec.
func ToArgv(s string) []string {
const (
InArg = iota
InArgQuote
OutOfArg
)
currentState := OutOfArg
currentQuoteChar := "\x00" // to distinguish between ' and " quotations
// this allows to use "foo'bar"
currentArg := ""
argv := []string{}
isQuote := func(c string) bool {
return c == `"` || c == `'`
}
isEscape := func(c string) bool {
return c == `\`
}
isWhitespace := func(c string) bool {
return c == " " || c == "\t"
}
L := len(s)
for i := 0; i < L; i++ {
c := s[i : i+1]
//fmt.Printf("c %s state %v arg %s argv %v i %d\n", c, currentState, currentArg, args, i)
if isQuote(c) {
switch currentState {
case OutOfArg:
currentArg = ""
fallthrough
case InArg:
currentState = InArgQuote
currentQuoteChar = c
case InArgQuote:
if c == currentQuoteChar {
currentState = InArg
} else {
currentArg += c
}
}
} else if isWhitespace(c) {
switch currentState {
case InArg:
argv = append(argv, currentArg)
currentState = OutOfArg
case InArgQuote:
currentArg += c
case OutOfArg:
// nothing
}
} else if isEscape(c) {
switch currentState {
case OutOfArg:
currentArg = ""
currentState = InArg
fallthrough
case InArg:
fallthrough
case InArgQuote:
if i == L-1 {
if runtime.GOOS == "windows" {
// just add \ to end for windows
currentArg += c
} else {
panic("Escape character at end string")
}
} else {
if runtime.GOOS == "windows" {
peek := s[i+1 : i+2]
if peek != `"` {
currentArg += c
}
} else {
i++
c = s[i : i+1]
currentArg += c
}
}
}
} else {
switch currentState {
case InArg, InArgQuote:
currentArg += c
case OutOfArg:
currentArg = ""
currentArg += c
currentState = InArg
}
}
}
if currentState == InArg {
argv = append(argv, currentArg)
} else if currentState == InArgQuote {
panic("Starting quote has no ending quote.")
}
return argv
}