Skip to content

Commit

Permalink
ls: fix incorrect query parsing with quotes escaped by the shell
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed May 1, 2022
1 parent edc8b75 commit b9991d8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
23 changes: 16 additions & 7 deletions commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ func runLs(env *Env, opts lsOptions, args []string) error {
var err error

if len(args) >= 1 {
// either the shell or cobra remove the quotes, we need them back for the parsing
for i, arg := range args {
if strings.Contains(arg, " ") {
args[i] = fmt.Sprintf("\"%s\"", arg)
}
}
assembled := strings.Join(args, " ")
// either the shell or cobra remove the quotes, we need them back for the query parsing
assembled := repairQuery(args)

q, err = query.Parse(assembled)
if err != nil {
return err
Expand Down Expand Up @@ -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 {
Id string `json:"id"`
HumanId string `json:"human_id"`
Expand Down
43 changes: 43 additions & 0 deletions commands/ls_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit b9991d8

Please sign in to comment.