Skip to content

Commit

Permalink
added feature required subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
ericjaystevens committed Aug 29, 2020
1 parent 6d95a12 commit 83482fe
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
38 changes: 23 additions & 15 deletions slashparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
type Argument struct {
Name string `yaml:"name" json:"name"`
ArgType string `yaml:"argtype" json:"argtype"`
Default string `yaml: "default" json: "default"`
Default string `yaml:"default" json:"default"`
Description string `yaml:"description" json:"description"`
ErrorMsg string `yaml:"errorMsg" json:"errorMsg"`
Position int `yaml:"position" json:"position"`
Expand All @@ -34,21 +34,23 @@ type Argument struct {

//SlashCommand defines the structure of a slash command string
type SlashCommand struct {
Name string `yaml:"name" json:"name,omitempty"`
Description string `yaml:"description" json:"description"`
Arguments []Argument `yaml:"arguments" json:"arguments,omitempty"`
SubCommands []SubCommand `yaml:"subcommands" json:"subcommands,omitempty"`
handler func(map[string]string) (string, error)
Name string `yaml:"name" json:"name,omitempty"`
Description string `yaml:"description" json:"description"`
Arguments []Argument `yaml:"arguments" json:"arguments,omitempty"`
SubCommands []SubCommand `yaml:"subcommands" json:"subcommands,omitempty"`
handler func(map[string]string) (string, error)
SubCommandRequired bool `yaml:"subCommandRequired" json:"subCommandRequired"`
}

//SubCommand defines a command that proceeded the slash command
type SubCommand struct {
Name string `yaml:"name" json:"name"`
Description string `yaml:"description" json:"description"`
Arguments []Argument `yaml:"arguments" json:"arguments"`
SubCommands []SubCommand `yaml:"subcommands" json:"subcommands"`
commandPaths []string
handler func(map[string]string) (string, error)
Name string `yaml:"name" json:"name"`
Description string `yaml:"description" json:"description"`
Arguments []Argument `yaml:"arguments" json:"arguments"`
SubCommands []SubCommand `yaml:"subcommands" json:"subcommands"`
commandPaths []string
handler func(map[string]string) (string, error)
SubCommandRequired bool `yaml:"subCommandRequired" json:"subCommandRequired"`
}

//implimented by SlashCommand and SubCommand
Expand Down Expand Up @@ -310,7 +312,7 @@ func (s *SlashCommand) getCommandString(args string) (commandString string, err
command := strings.Replace(argsSplit[0], "/", "", 1)
args = strings.Replace(args, "/", "", 1)

//check each subcommand
//check sub subcommand
for _, subCommand := range s.SubCommands {
for _, subSubCommand := range subCommand.SubCommands {
subCommandString := s.Name + " " + subCommand.Name + " " + subSubCommand.Name
Expand All @@ -321,20 +323,26 @@ func (s *SlashCommand) getCommandString(args string) (commandString string, err
}
}

//check each sub sub command
//check each subcommand
subCommandString := s.Name + " " + subCommand.Name
if len(args) >= len(subCommandString) {
if strings.EqualFold(args[:len(subCommandString)], subCommandString) {
if subCommand.SubCommandRequired {
return "", fmt.Errorf("/%s is not a valid command. Please see /%s help", subCommandString, s.Name)
}
return subCommandString, nil
}
}
}

if strings.EqualFold(command, s.Name) {
if s.SubCommandRequired {
return "", fmt.Errorf("/%s is not a valid command. Please see /%s help", s.Name, s.Name)
}
return s.Name, nil
}

return "", errors.New(command + " is not a valid command")
return "", fmt.Errorf("/%s is not a valid command. Please see /%s help", command, s.Name)
}

//Parse parse the command string
Expand Down
57 changes: 50 additions & 7 deletions slashparse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slashparse

import (
"errors"
"io/ioutil"
"strings"
"testing"
Expand Down Expand Up @@ -340,17 +341,59 @@ func TestGetValues(t *testing.T) {
}
}

type testParseTest struct {
testName string
commandString string
slashDef []byte
wantCommandString string
wantValues map[string]string
wantError error
}

func TestParse(t *testing.T) {
slashCommandString := "/print foo"

wantCommands := "Print"
wantValues := map[string]string{"text": "foo"}
tests := []testParseTest{
{
testName: "simple test",
commandString: "/print foo",
slashDef: SimpleDef,
wantCommandString: "Print",
wantValues: map[string]string{"text": "foo"},
},
{
testName: "valid wrangler command",
commandString: "/wrangler list channels",
slashDef: wranglerDef,
wantCommandString: "wrangler list channels",
wantValues: map[string]string{},
},
{
testName: "missing required subsubcommand",
commandString: "/wrangler list",
slashDef: wranglerDef,
wantError: errors.New("/wrangler list is not a valid command. Please see /wrangler help"),
},
{
testName: "missing required subcommand",
commandString: "/wrangler",
slashDef: wranglerDef,
wantError: errors.New("/wrangler is not a valid command. Please see /wrangler help"),
},
}

newSlash, _ := NewSlashCommand(SimpleDef)
gotCommands, gotValues, _ := newSlash.Parse(slashCommandString)
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {

assert.Equal(t, gotCommands, wantCommands)
assert.Equal(t, gotValues, wantValues)
newSlash, _ := NewSlashCommand(test.slashDef)
gotCommands, gotValues, gotErr := newSlash.Parse(test.commandString)

assert.Equal(t, test.wantCommandString, gotCommands)
assert.Equal(t, test.wantValues, gotValues)
if test.wantError != nil {
assert.EqualError(t, gotErr, test.wantError.Error())
}
})
}
}

//TODO: move simple2 to a test data folder, create more test yaml some that should validate and some that shouldn't
Expand Down
5 changes: 5 additions & 0 deletions testData/wrangler.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
---
name: wrangler
description: Manage Mattermost Messages Masterfully
subCommandRequired: true
subcommands:
- name: info
description: Shows plugin information
- name: move
description: Move a message
subCommandRequired: true
subcommands:
- name: thread
description: "Move a message and the thread it belongs to"
Expand All @@ -24,6 +26,7 @@ subcommands:
position: 1
- name: copy
description: Copy messages
subCommandRequired: true
subcommands:
- name: thread
description: Copy a message and the thread it belongs to
Expand All @@ -42,6 +45,7 @@ subcommands:
position: 1
- name: attach
description: Attach messages
subCommandRequired: true
subcommands:
- name: message
description: Attach messages
Expand All @@ -60,6 +64,7 @@ subcommands:
position: 1
- name: list
description: Lists IDs for channels and messages
subCommandRequired: true
subcommands:
- name: channels
description: List channel IDs that you have joined
Expand Down

0 comments on commit 83482fe

Please sign in to comment.