2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2020-01-26 07:17:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Break is a non-fatal error that could be returned from middlewares to stop
|
|
|
|
// the chain of execution.
|
|
|
|
var Break = errors.New("break middleware chain, non-fatal")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// filterEventType filters all commands and subcommands into a 2D slice,
|
|
|
|
// structured so that a Break would only exit out the nested slice.
|
|
|
|
func (ctx *Context) filterEventType(evT reflect.Type) (callers [][]caller) {
|
2020-04-09 20:49:12 +00:00
|
|
|
// Find the main context first.
|
2020-05-10 08:45:00 +00:00
|
|
|
callers = append(callers, ctx.eventCallers(evT))
|
2020-04-09 20:49:12 +00:00
|
|
|
|
|
|
|
for _, sub := range ctx.subcommands {
|
|
|
|
// Find subcommands second.
|
2020-05-10 08:45:00 +00:00
|
|
|
callers = append(callers, sub.eventCallers(evT))
|
2020-04-09 20:49:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
return
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (ctx *Context) callCmd(ev interface{}) (bottomError error) {
|
|
|
|
evV := reflect.ValueOf(ev)
|
|
|
|
evT := evV.Type()
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
var callers [][]caller
|
2020-01-24 03:17:03 +00:00
|
|
|
|
|
|
|
// Hit the cache
|
|
|
|
t, ok := ctx.typeCache.Load(evT)
|
|
|
|
if ok {
|
2020-05-10 08:45:00 +00:00
|
|
|
callers = t.([][]caller)
|
2020-01-24 03:17:03 +00:00
|
|
|
} else {
|
|
|
|
callers = ctx.filterEventType(evT)
|
|
|
|
ctx.typeCache.Store(evT, callers)
|
|
|
|
}
|
2020-01-23 07:20:24 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
for _, subcallers := range callers {
|
|
|
|
for _, c := range subcallers {
|
|
|
|
_, err := c.call(evV)
|
|
|
|
if err != nil {
|
|
|
|
// Only count as an error if it's not Break.
|
|
|
|
if err = errNoBreak(err); err != nil {
|
|
|
|
bottomError = err
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Break the caller loop only for this subcommand.
|
|
|
|
break
|
2020-04-09 20:49:12 +00:00
|
|
|
}
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:52:15 +00:00
|
|
|
var msc *gateway.MessageCreateEvent
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// We call the messages later, since we want MessageCreate middlewares to
|
|
|
|
// run as well.
|
2020-05-15 01:52:15 +00:00
|
|
|
switch {
|
|
|
|
case evT == typeMessageCreate:
|
|
|
|
msc = ev.(*gateway.MessageCreateEvent)
|
|
|
|
|
|
|
|
case evT == typeMessageUpdate && ctx.EditableCommands:
|
|
|
|
up := ev.(*gateway.MessageUpdateEvent)
|
|
|
|
// Message updates could have empty contents when only their embeds are
|
|
|
|
// filled. We don't need that here.
|
|
|
|
if up.Content == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the updated message.
|
2020-11-30 01:19:59 +00:00
|
|
|
m, err := ctx.Cabinet.Message(up.ChannelID, up.ID)
|
2020-05-10 08:45:00 +00:00
|
|
|
if err != nil {
|
2020-05-15 01:52:15 +00:00
|
|
|
// It's probably safe to ignore this.
|
|
|
|
return nil
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
2020-05-15 01:52:15 +00:00
|
|
|
|
|
|
|
// Treat the message update as a message create event to avoid breaking
|
|
|
|
// changes.
|
|
|
|
msc = &gateway.MessageCreateEvent{Message: *m, Member: up.Member}
|
|
|
|
|
|
|
|
// Fill up member, if available.
|
2020-07-29 20:10:17 +00:00
|
|
|
if m.GuildID.IsValid() && up.Member == nil {
|
2020-11-30 01:19:59 +00:00
|
|
|
if mem, err := ctx.Cabinet.Member(m.GuildID, m.Author.ID); err == nil {
|
2020-05-15 01:52:15 +00:00
|
|
|
msc.Member = mem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the reflect value as well.
|
|
|
|
evV = reflect.ValueOf(msc)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Unknown event, return.
|
|
|
|
return nil
|
2020-01-25 22:30:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:52:15 +00:00
|
|
|
// There's no need for an errNoBreak here, as the method already checked
|
|
|
|
// for that.
|
|
|
|
return ctx.callMessageCreate(msc, evV)
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-02-18 19:08:21 +00:00
|
|
|
func (ctx *Context) callMessageCreate(
|
|
|
|
mc *gateway.MessageCreateEvent, value reflect.Value) error {
|
|
|
|
|
|
|
|
v, err := ctx.callMessageCreateNoReply(mc, value)
|
|
|
|
if err == nil && v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 20:09:10 +00:00
|
|
|
if err != nil && !ctx.ReplyError && ctx.ErrorReplier == nil {
|
2021-02-18 19:08:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var data api.SendMessageData
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-08 20:09:10 +00:00
|
|
|
if ctx.ErrorReplier != nil {
|
|
|
|
data = ctx.ErrorReplier(err, mc)
|
|
|
|
} else {
|
|
|
|
data.Content = ctx.FormatError(err)
|
|
|
|
}
|
2021-02-18 19:08:21 +00:00
|
|
|
} else {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
data.Content = v
|
|
|
|
case *discord.Embed:
|
|
|
|
data.Embed = v
|
|
|
|
case *api.SendMessageData:
|
|
|
|
data = *v
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Reference == nil {
|
|
|
|
data.Reference = &discord.MessageReference{MessageID: mc.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.AllowedMentions == nil {
|
|
|
|
// Do not mention on reply by default. Only allow author mentions.
|
|
|
|
data.AllowedMentions = &api.AllowedMentions{
|
|
|
|
Users: []discord.UserID{mc.Author.ID},
|
|
|
|
RepliedUser: option.False,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ctx.SendMessageComplex(mc.ChannelID, data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) callMessageCreateNoReply(
|
|
|
|
mc *gateway.MessageCreateEvent, value reflect.Value) (interface{}, error) {
|
|
|
|
|
2020-04-10 04:46:21 +00:00
|
|
|
// check if bot
|
|
|
|
if !ctx.AllowBot && mc.Author.Bot {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, nil
|
2020-04-10 04:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
// check if prefix
|
2020-04-06 20:25:42 +00:00
|
|
|
pf, ok := ctx.HasPrefix(mc)
|
|
|
|
if !ok {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
// trim the prefix before splitting, this way multi-words prefixes work
|
2020-04-06 20:25:42 +00:00
|
|
|
content := mc.Content[len(pf):]
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
if content == "" {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, nil // just the prefix only
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse arguments
|
2020-08-11 20:44:10 +00:00
|
|
|
parts, parseErr := ctx.ParseArgs(content)
|
|
|
|
// We're not checking parse errors yet, as raw arguments may be able to
|
|
|
|
// ignore it.
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
if len(parts) == 0 {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, parseErr
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Find the command and subcommand.
|
2020-12-18 23:00:21 +00:00
|
|
|
commandCtx, err := ctx.findCommand(parts)
|
2020-05-10 08:45:00 +00:00
|
|
|
if err != nil {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, errNoBreak(err)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:00:21 +00:00
|
|
|
var (
|
|
|
|
arguments = commandCtx.parts
|
|
|
|
cmd = commandCtx.method
|
|
|
|
sub = commandCtx.subcmd
|
|
|
|
plumbed = commandCtx.plumbed
|
|
|
|
)
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// We don't run the subcommand's middlewares here, as the callCmd function
|
|
|
|
// already handles that.
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Run command middlewares.
|
|
|
|
if err := cmd.walkMiddlewares(value); err != nil {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, errNoBreak(err)
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
// Start converting
|
|
|
|
var argv []reflect.Value
|
2020-05-04 01:33:24 +00:00
|
|
|
var argc int
|
|
|
|
|
|
|
|
// the last argument in the list, not used until set
|
|
|
|
var last Argument
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-01-25 22:07:49 +00:00
|
|
|
// Here's an edge case: when the handler takes no arguments, we allow that
|
|
|
|
// anyway, as they might've used the raw content.
|
2020-05-14 03:42:31 +00:00
|
|
|
if len(cmd.Arguments) == 0 {
|
2021-02-18 19:08:21 +00:00
|
|
|
return cmd.call(value, argv...)
|
2020-01-25 22:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
// Argument count check.
|
2020-05-04 01:33:24 +00:00
|
|
|
if argdelta := len(arguments) - len(cmd.Arguments); argdelta != 0 {
|
2020-05-03 22:59:10 +00:00
|
|
|
var err error // no err if nil
|
|
|
|
|
2020-05-04 04:22:19 +00:00
|
|
|
// If the function is variadic, then we can allow the last argument to
|
|
|
|
// be empty.
|
|
|
|
if cmd.Variadic {
|
|
|
|
argdelta++
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
switch {
|
|
|
|
// If there aren't enough arguments given.
|
|
|
|
case argdelta < 0:
|
|
|
|
err = ErrNotEnoughArgs
|
|
|
|
|
|
|
|
// If there are too many arguments, then check if the command supports
|
|
|
|
// variadic arguments. We already did a length check above.
|
|
|
|
case argdelta > 0 && !cmd.Variadic:
|
|
|
|
// If it's not variadic, then we can't accept it.
|
|
|
|
err = ErrTooManyArgs
|
2020-01-26 06:15:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
if err != nil {
|
2021-06-06 19:40:24 +00:00
|
|
|
return nil, &InvalidUsageError{
|
2020-05-03 22:59:10 +00:00
|
|
|
Prefix: pf,
|
2020-05-04 01:33:24 +00:00
|
|
|
Args: parts,
|
|
|
|
Index: len(parts) - 1,
|
2020-05-03 22:59:10 +00:00
|
|
|
Wrap: err,
|
|
|
|
Ctx: cmd,
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// The last argument in the arguments slice.
|
|
|
|
last = cmd.Arguments[len(cmd.Arguments)-1]
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
// Allocate a new slice the length of function arguments.
|
2020-05-04 01:33:24 +00:00
|
|
|
argc = len(cmd.Arguments) - 1 // arg len without last
|
|
|
|
argv = make([]reflect.Value, 0, argc) // could be 0
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Parse all arguments except for the last one.
|
|
|
|
for i := 0; i < argc; i++ {
|
|
|
|
v, err := cmd.Arguments[i].fn(arguments[0])
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
2021-06-06 19:40:24 +00:00
|
|
|
return nil, &InvalidUsageError{
|
2020-05-03 22:59:10 +00:00
|
|
|
Prefix: pf,
|
2020-05-04 01:33:24 +00:00
|
|
|
Args: parts,
|
|
|
|
Index: len(parts) - len(arguments) + i,
|
2020-05-03 22:59:10 +00:00
|
|
|
Wrap: err,
|
|
|
|
Ctx: cmd,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Pop arguments.
|
|
|
|
arguments = arguments[1:]
|
|
|
|
argv = append(argv, v)
|
2020-05-03 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Is this last argument actually a variadic slice? If yes, then it
|
|
|
|
// should still have fn normally.
|
|
|
|
if last.fn != nil {
|
|
|
|
// Allocate a new slice to append into.
|
|
|
|
vars := make([]reflect.Value, 0, len(arguments))
|
2020-05-03 22:59:10 +00:00
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Parse the rest with variadic arguments. Go's reflect states that
|
2020-07-29 23:29:01 +00:00
|
|
|
// variadic parameters will automatically be copied, which is good.
|
2020-05-04 01:33:24 +00:00
|
|
|
for i := 0; len(arguments) > 0; i++ {
|
|
|
|
v, err := last.fn(arguments[0])
|
2020-05-03 22:59:10 +00:00
|
|
|
if err != nil {
|
2021-06-06 19:40:24 +00:00
|
|
|
return nil, &InvalidUsageError{
|
2020-05-03 22:59:10 +00:00
|
|
|
Prefix: pf,
|
2020-05-04 01:33:24 +00:00
|
|
|
Args: parts,
|
|
|
|
Index: len(parts) - len(arguments) + i,
|
2020-05-03 22:59:10 +00:00
|
|
|
Wrap: err,
|
|
|
|
Ctx: cmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
arguments = arguments[1:]
|
|
|
|
vars = append(vars, v)
|
2020-05-03 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
argv = append(argv, vars...)
|
2020-05-04 01:33:24 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Create a zero value instance of this:
|
|
|
|
v := reflect.New(last.rtype)
|
2021-02-18 19:08:21 +00:00
|
|
|
var err error // return nil, error
|
2020-05-04 01:33:24 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
// If the argument wants all arguments:
|
|
|
|
case last.manual != nil:
|
|
|
|
// Call the manual parse method:
|
2020-11-15 05:05:31 +00:00
|
|
|
err = last.manual(v.Interface().(ManualParser), arguments)
|
2020-05-04 01:33:24 +00:00
|
|
|
|
|
|
|
// If the argument wants all arguments in string:
|
|
|
|
case last.custom != nil:
|
2020-08-11 20:44:10 +00:00
|
|
|
// Ignore parser errors. This allows custom commands sliced away to
|
|
|
|
// have erroneous hanging quotes.
|
|
|
|
parseErr = nil
|
|
|
|
|
2020-11-15 06:27:55 +00:00
|
|
|
content = trimPrefixStringAndSlice(content, sub.Command, sub.Aliases)
|
2020-08-11 20:44:10 +00:00
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
// If the current command is not the plumbed command, then we can
|
|
|
|
// keep trimming. We have to check for this, as a plumbed subcommand
|
2021-02-18 19:08:21 +00:00
|
|
|
// may return nil, other non-plumbed commands.
|
2020-12-18 23:00:21 +00:00
|
|
|
if !plumbed {
|
2020-11-15 06:27:55 +00:00
|
|
|
content = trimPrefixStringAndSlice(content, cmd.Command, cmd.Aliases)
|
2020-05-04 01:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method with the raw unparsed command:
|
2020-11-15 05:05:31 +00:00
|
|
|
err = last.custom(v.Interface().(CustomParser), content)
|
2020-05-04 01:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the returned error:
|
|
|
|
if err != nil {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, err
|
2020-05-04 01:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the argument wants a non-pointer:
|
|
|
|
if last.pointer {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the argument into argv.
|
|
|
|
argv = append(argv, v)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:44:10 +00:00
|
|
|
// Check for parsing errors after parsing arguments.
|
|
|
|
if parseErr != nil {
|
2021-02-18 19:08:21 +00:00
|
|
|
return nil, parseErr
|
2021-02-14 20:39:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 19:08:21 +00:00
|
|
|
return cmd.call(value, argv...)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:00:21 +00:00
|
|
|
// commandContext contains related command values to call one. It is returned
|
|
|
|
// from findCommand.
|
|
|
|
type commandContext struct {
|
|
|
|
parts []string
|
|
|
|
plumbed bool
|
|
|
|
method *MethodContext
|
|
|
|
subcmd *Subcommand
|
|
|
|
}
|
|
|
|
|
|
|
|
var emptyCommand = commandContext{}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// findCommand filters.
|
2020-12-18 23:00:21 +00:00
|
|
|
func (ctx *Context) findCommand(parts []string) (commandContext, error) {
|
2020-05-10 08:45:00 +00:00
|
|
|
// Main command entrypoint cannot have plumb.
|
2020-05-13 04:37:06 +00:00
|
|
|
for _, c := range ctx.Commands {
|
2020-11-13 03:02:52 +00:00
|
|
|
if searchStringAndSlice(parts[0], c.Command, c.Aliases) {
|
2020-12-18 23:00:21 +00:00
|
|
|
return commandContext{parts[1:], false, c, ctx.Subcommand}, nil
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Can't find the command, look for subcommands if len(args) has a 2nd
|
|
|
|
// entry.
|
|
|
|
for _, s := range ctx.subcommands {
|
2020-11-13 03:02:52 +00:00
|
|
|
if !searchStringAndSlice(parts[0], s.Command, s.Aliases) {
|
2020-05-10 08:45:00 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
// The new plumbing behavior allows other commands to co-exist with a
|
|
|
|
// plumbed command. Those commands will override the second argument,
|
|
|
|
// similarly to a non-plumbed command.
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
if len(parts) >= 2 {
|
2020-05-13 04:37:06 +00:00
|
|
|
for _, c := range s.Commands {
|
2020-11-13 03:02:52 +00:00
|
|
|
if searchStringAndSlice(parts[1], c.Command, c.Aliases) {
|
2020-12-18 23:00:21 +00:00
|
|
|
return commandContext{parts[2:], false, c, s}, nil
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-11-30 22:26:53 +00:00
|
|
|
if s.IsPlumbed() {
|
2020-12-18 23:00:21 +00:00
|
|
|
return commandContext{parts[1:], true, s.plumbed, s}, nil
|
2020-11-30 22:26:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// If unknown command is disabled or the subcommand is hidden:
|
|
|
|
if ctx.SilentUnknown.Subcommand || s.Hidden {
|
2020-12-18 23:00:21 +00:00
|
|
|
return emptyCommand, Break
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
2020-01-23 07:20:24 +00:00
|
|
|
|
2020-12-18 23:00:21 +00:00
|
|
|
return emptyCommand, newErrUnknownCommand(s, parts)
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
if ctx.SilentUnknown.Command {
|
2020-12-18 23:00:21 +00:00
|
|
|
return emptyCommand, Break
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:00:21 +00:00
|
|
|
return emptyCommand, newErrUnknownCommand(ctx.Subcommand, parts)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
// searchStringAndSlice searches if str is equal to isString or any of the given
|
|
|
|
// otherStrings. It is used for alias matching.
|
|
|
|
func searchStringAndSlice(str string, isString string, otherStrings []string) bool {
|
|
|
|
if str == isString {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, other := range otherStrings {
|
|
|
|
if other == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-15 06:27:55 +00:00
|
|
|
// trimPrefixStringAndSlice behaves similarly to searchStringAndSlice, but it
|
|
|
|
// trims the prefix and the surrounding spaces after a match.
|
|
|
|
func trimPrefixStringAndSlice(str string, prefix string, prefixes []string) string {
|
|
|
|
if strings.HasPrefix(str, prefix) {
|
|
|
|
return strings.TrimSpace(str[len(prefix):])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range prefixes {
|
|
|
|
if strings.HasPrefix(str, prefix) {
|
|
|
|
return strings.TrimSpace(str[len(prefix):])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func errNoBreak(err error) error {
|
|
|
|
if errors.Is(err, Break) {
|
|
|
|
return nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-05-10 08:45:00 +00:00
|
|
|
return err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|