Skip to content

Commit

Permalink
text: improves ToArgv to handle unfinished quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Oct 27, 2019
1 parent 5152239 commit aacc5d2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
4 changes: 2 additions & 2 deletions events/event_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (e *Event) Parse() {
e.botMention = true
}

args, err := text.ToArgv(content[len(e.Prefix()):])
if err != nil || len(args) <= 0 {
args := text.ToArgv(content[len(e.Prefix()):])
if len(args) <= 0 {
return
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
go.opencensus.io v0.22.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
Expand Down
13 changes: 3 additions & 10 deletions text/argv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package text

import (
"errors"
"runtime"
"strings"
)
Expand All @@ -10,7 +9,7 @@ import (
var quoteReplacer = strings.NewReplacer("“", "\"", "”", "\"", "‘", "'", "’", "'")

// ToArgv converts string s into an string array text in quotes will be counted as one array element
func ToArgv(s string) ([]string, error) {
func ToArgv(s string) []string {
s = quoteReplacer.Replace(s)

const (
Expand Down Expand Up @@ -82,8 +81,6 @@ func ToArgv(s string) ([]string, error) {
if runtime.GOOS == "windows" {
// just add \ to end for windows
currentArg += c
} else {
return nil, errors.New("escape character at end string")
}
} else {
if runtime.GOOS == "windows" {
Expand Down Expand Up @@ -111,11 +108,7 @@ func ToArgv(s string) ([]string, error) {
}
}

if currentState == InArg {
argv = append(argv, currentArg)
} else if currentState == InArgQuote {
return nil, errors.New("starting quote has no ending quote")
}
argv = append(argv, currentArg)

return argv, nil
return argv
}
58 changes: 58 additions & 0 deletions text/argv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package text

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestToArgv(t *testing.T) {
tests := []struct {
name string
give string
want []string
}{
{
name: "command without quotes",
give: "_test a b c",
want: []string{"_test", "a", "b", "c"},
},
{
name: "command with quotes",
give: "_test \"my long\" \"command with quotes\"",
want: []string{"_test", "my long", "command with quotes"},
},
{
name: "different type of quotes",
give: "_test “my long“ 'command with quotes'",
want: []string{"_test", "my long", "command with quotes"},
},
{
name: "mixed quotes and no quotes",
give: "_test \"my long\" abc \"command with quotes\"",
want: []string{"_test", "my long", "abc", "command with quotes"},
},
{
name: "unfinished quotes",
give: "_test \"my long\" abc \"command that's not finished",
want: []string{"_test", "my long", "abc", "command that's not finished"},
},
{
name: "escape character at the end",
give: "_test \"my long\" abc \"command with quotes\"\\",
want: []string{"_test", "my long", "abc", "command with quotes"},
},
{
name: "quote at the beginning",
give: "\"_test\" \"my long\" abc \"command with quotes\"",
want: []string{"_test", "my long", "abc", "command with quotes"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := ToArgv(tt.give)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit aacc5d2

Please sign in to comment.