diff --git a/events/event_command.go b/events/event_command.go index 274b9a9..048932b 100644 --- a/events/event_command.go +++ b/events/event_command.go @@ -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 } diff --git a/go.mod b/go.mod index 2f0e18d..d0adeba 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/text/argv.go b/text/argv.go index ab5948e..82698e4 100644 --- a/text/argv.go +++ b/text/argv.go @@ -1,7 +1,6 @@ package text import ( - "errors" "runtime" "strings" ) @@ -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 ( @@ -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" { @@ -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 } diff --git a/text/argv_test.go b/text/argv_test.go new file mode 100644 index 0000000..2117bac --- /dev/null +++ b/text/argv_test.go @@ -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) + }) + } +}