ls: fix incorrect query parsing with quotes escaped by the shell

This commit is contained in:
Michael Muré 2022-05-01 12:30:55 +02:00
parent edc8b7589d
commit b9991d84b9
No known key found for this signature in database
GPG Key ID: A4457C029293126F
2 changed files with 59 additions and 7 deletions

View File

@ -103,13 +103,9 @@ func runLs(env *Env, opts lsOptions, args []string) error {
var err error var err error
if len(args) >= 1 { if len(args) >= 1 {
// either the shell or cobra remove the quotes, we need them back for the parsing // either the shell or cobra remove the quotes, we need them back for the query parsing
for i, arg := range args { assembled := repairQuery(args)
if strings.Contains(arg, " ") {
args[i] = fmt.Sprintf("\"%s\"", arg)
}
}
assembled := strings.Join(args, " ")
q, err = query.Parse(assembled) q, err = query.Parse(assembled)
if err != nil { if err != nil {
return err return err
@ -153,6 +149,19 @@ func runLs(env *Env, opts lsOptions, args []string) error {
} }
} }
func repairQuery(args []string) string {
for i, arg := range args {
split := strings.Split(arg, ":")
for j, s := range split {
if strings.Contains(s, " ") {
split[j] = fmt.Sprintf("\"%s\"", s)
}
}
args[i] = strings.Join(split, ":")
}
return strings.Join(args, " ")
}
type JSONBugExcerpt struct { type JSONBugExcerpt struct {
Id string `json:"id"` Id string `json:"id"`
HumanId string `json:"human_id"` HumanId string `json:"human_id"`

43
commands/ls_test.go Normal file
View File

@ -0,0 +1,43 @@
package commands
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_repairQuery(t *testing.T) {
cases := []struct {
args []string
output string
}{
{
[]string{""},
"",
},
{
[]string{"foo"},
"foo",
},
{
[]string{"foo", "bar"},
"foo bar",
},
{
[]string{"foo bar", "baz"},
"\"foo bar\" baz",
},
{
[]string{"foo:bar", "baz"},
"foo:bar baz",
},
{
[]string{"foo:bar boo", "baz"},
"foo:\"bar boo\" baz",
},
}
for _, tc := range cases {
require.Equal(t, tc.output, repairQuery(tc.args))
}
}