forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_handler.go
41 lines (36 loc) · 892 Bytes
/
command_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package session
import (
"github.com/bettercap/readline"
"regexp"
"sync"
)
type CommandHandler struct {
sync.Mutex
Name string
Description string
Completer *readline.PrefixCompleter
Parser *regexp.Regexp
exec func(args []string, s *Session) error
}
func NewCommandHandler(name string, expr string, desc string, exec func(args []string, s *Session) error) CommandHandler {
return CommandHandler{
Name: name,
Description: desc,
Completer: nil,
Parser: regexp.MustCompile(expr),
exec: exec,
}
}
func (h *CommandHandler) Parse(line string) (bool, []string) {
result := h.Parser.FindStringSubmatch(line)
if len(result) == h.Parser.NumSubexp()+1 {
return true, result[1:]
} else {
return false, nil
}
}
func (h *CommandHandler) Exec(args []string, s *Session) error {
h.Lock()
defer h.Unlock()
return h.exec(args, s)
}