1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-23 00:39:13 +02:00

Removed package github.com/mgutz/str for better code coverage

This commit is contained in:
mjarkk
2018-10-27 17:06:33 +02:00
parent ed564adb4a
commit 18f09a14e6
7 changed files with 114 additions and 915 deletions

View File

@ -10,7 +10,6 @@ import (
"regexp"
"github.com/kr/pty"
"github.com/mgutz/str"
)
// RunCommandWithOutputLiveWrapper runs a command and return every word that gets written in stdout
@ -19,7 +18,7 @@ import (
// NOTE: If the return data is empty it won't written anything to stdin
// NOTE: You don't have to include a enter in the return data this function will do that for you
func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error {
splitCmd := str.ToArgv(command)
splitCmd := ToArgv(command)
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
cmd.Env = os.Environ()

View File

@ -9,7 +9,6 @@ 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"
)
@ -50,7 +49,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 := str.ToArgv(command)
splitCmd := ToArgv(command)
c.Log.Info(splitCmd)
return sanitisedCommandOutput(
c.command(splitCmd[0], splitCmd[1:]...).CombinedOutput(),

View File

@ -0,0 +1,112 @@
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
}