-
Notifications
You must be signed in to change notification settings - Fork 5
/
commandManager.go
52 lines (44 loc) · 1020 Bytes
/
commandManager.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
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"errors"
"io"
"strings"
)
type processor func(args []string, out io.Writer, state *State) error
type hndler struct {
funcs map[string]processor
state *State
}
type State struct {
Files []FakeFile //fake files which are "introduced to the session"
History []string //listing of previous commands for the '!!' and what not
}
type FakeFile struct {
Path string
Content string
}
func NewHandler() *hndler {
return &hndler{
funcs: make(map[string]processor, 3),
}
}
func (h *hndler) Register(command string, proc processor) error {
_, ok := h.funcs[command]
if ok {
return errors.New("Already registered")
}
h.funcs[command] = proc
return nil
}
func (h *hndler) Handle(cmd string, args []string, out io.Writer) (bool, error) {
hnd, ok := h.funcs[cmd]
if !ok {
//no handler ready for that command
return false, nil
}
err := hnd(args, out, h.state)
if err != nil {
h.state.History = append(h.state.History, cmd+strings.Join(args, " "))
}
return true, err
}