This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
/
command.go
75 lines (64 loc) · 1.66 KB
/
command.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package router
import (
"fmt"
"strconv"
"strings"
"time"
)
type ActionType string
const (
AddAction ActionType = "add"
DelAction ActionType = "del"
DeleteAction ActionType = "delete"
ListAction ActionType = "list"
HelpAction ActionType = "help"
)
type Command struct {
Trigger string // router
Action ActionType
FilterArgs string
RouterID string
}
func generateRouterID() string {
return strconv.FormatUint(uint64(time.Now().Unix()), 36)
}
// Scan router add hoge.* .*fuga.* #random -> cronCommand
func (c *Command) Scan(command string) (bool, error) {
commands := strings.Fields(command)
if len(commands) < 2 {
return false, nil
}
c.Trigger = commands[0]
c.Action = ActionType(commands[1])
switch c.Action {
case AddAction:
// router add hoge.* .*fuga.* #random
if len(commands) < 4 || 5 < len(commands) {
return false, fmt.Errorf("commands length error %d", len(commands))
}
c.FilterArgs = strings.Join(commands[2:], " ")
c.RouterID = generateRouterID()
case DelAction, DeleteAction:
// router del xxxxxxxxx
if len(commands) != 3 {
return false, fmt.Errorf("commands length error %d", len(commands))
}
c.RouterID = commands[2]
case ListAction, HelpAction:
// router list
if len(commands) != 2 {
return false, fmt.Errorf("commands length error %d", len(commands))
}
}
return true, nil
}
func (c Command) String() string {
return fmt.Sprintf("%s %s %s %s", c.Trigger, c.Action, c.FilterArgs, c.RouterID)
}
// Message return a reply message
func (c Command) Message() string {
return fmt.Sprintf("FilterArgs = [%s] ID = [%s]", c.FilterArgs, c.RouterID)
}
func (c Command) Key() string {
return c.RouterID
}