fix: parse cmd error with single quotes
This commit is contained in:
parent
a9c7cb1cd2
commit
2388f309d8
|
@ -135,29 +135,38 @@ func SplitCmd(cmd string) []string {
|
||||||
var result []string
|
var result []string
|
||||||
var curStr strings.Builder
|
var curStr strings.Builder
|
||||||
var preChar int32
|
var preChar int32
|
||||||
inQuotes := false
|
var quotesChar int32
|
||||||
|
|
||||||
for _, char := range []rune(cmd) {
|
cmdRune := []rune(cmd)
|
||||||
if char == '"' && preChar != '\\' {
|
for _, char := range cmdRune {
|
||||||
inQuotes = !inQuotes
|
if (char == '"' || char == '\'') && preChar != '\\' && (quotesChar == 0 || quotesChar == char) {
|
||||||
} else if char == ' ' && !inQuotes {
|
if quotesChar != 0 {
|
||||||
if curStr.Len() > 0 {
|
quotesChar = 0
|
||||||
if part, e := strconv.Unquote(`"` + curStr.String() + `"`); e == nil {
|
} else {
|
||||||
result = append(result, part)
|
quotesChar = char
|
||||||
}
|
|
||||||
curStr.Reset()
|
|
||||||
}
|
}
|
||||||
|
} else if char == ' ' && quotesChar == 0 {
|
||||||
|
result = append(result, curStr.String())
|
||||||
|
curStr.Reset()
|
||||||
} else {
|
} else {
|
||||||
curStr.WriteRune(char)
|
curStr.WriteRune(char)
|
||||||
}
|
}
|
||||||
preChar = char
|
preChar = char
|
||||||
}
|
}
|
||||||
|
result = append(result, curStr.String())
|
||||||
|
|
||||||
if curStr.Len() > 0 {
|
result = sliceutil.FilterMap(result, func(i int) (string, bool) {
|
||||||
if part, e := strconv.Unquote(`"` + curStr.String() + `"`); e == nil {
|
var part = strings.TrimSpace(result[i])
|
||||||
result = append(result, part)
|
if len(part) <= 0 {
|
||||||
|
return "", false
|
||||||
}
|
}
|
||||||
}
|
if strings.Contains(part, "\\") {
|
||||||
|
if unquotePart, e := strconv.Unquote(`"` + part + `"`); e == nil {
|
||||||
|
return unquotePart, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return part, true
|
||||||
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue