fix: command handlers are now atomically locked

This commit is contained in:
evilsocket 2019-02-26 13:26:28 +01:00
commit e46ea6c9a9
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
3 changed files with 21 additions and 12 deletions

View file

@ -3,14 +3,16 @@ 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
exec func(args []string, s *Session) error
}
func NewCommandHandler(name string, expr string, desc string, exec func(args []string, s *Session) error) CommandHandler {
@ -19,7 +21,7 @@ func NewCommandHandler(name string, expr string, desc string, exec func(args []s
Description: desc,
Completer: nil,
Parser: regexp.MustCompile(expr),
Exec: exec,
exec: exec,
}
}
@ -31,3 +33,9 @@ func (h *CommandHandler) Parse(line string) (bool, []string) {
return false, nil
}
}
func (h *CommandHandler) Exec(args []string, s *Session) error {
h.Lock()
defer h.Unlock()
return h.exec(args, s)
}