forked from yurigorokhov/mtflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
133 lines (112 loc) · 2.97 KB
/
parse.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"errors"
"log"
"strings"
)
const trimSymbols = " !#$%^&*()~<>?,"
// CommandType is a top level command that needs to have a target
// associated with it to be applied
type CommandType uint
const (
// CommandNone is a wild card to detect unknown command
CommandNone CommandType = iota
// CommandStart command starts 'something'
CommandStart
// CommandStop command stops 'something'
CommandStop
// CommandStatus command gets the status of 'something'
CommandStatus
// CommandFortune command gets you that
CommandFortune
)
func (commandType CommandType) String() string {
switch commandType {
case CommandNone:
return "none"
case CommandStart:
return "start"
case CommandStop:
return "stop"
case CommandStatus:
return "status"
case CommandFortune:
return "fortune"
default:
log.Println("CommandType switch not considering all cases!")
return ""
}
}
// CommandTarget is the set of possible targets for a command
type CommandTarget uint
const (
// CommandTargetNone is a wild card used to detect an unhandled target
CommandTargetNone CommandTarget = iota
// CommandTargetPR is the PullRequestService target name
CommandTargetPR
// CommandTargetMtFlow is this program :)
CommandTargetMtFlow
)
func (commandTarget CommandTarget) String() string {
switch commandTarget {
case CommandTargetNone:
return "none"
case CommandTargetPR:
return "pr"
case CommandTargetMtFlow:
return "mtflow"
default:
log.Println("CommandTarget switch not considering all cases!")
return ""
}
}
// Command is the metadata about the type, target, and whom the
// command was issued to
type Command struct {
Type CommandType
Target CommandTarget
Mentions []string
ThreadId string
}
// ParseCommand takes care of turning a text string into a proper command
func ParseCommand(commandStr string, threadId string) (*Command, error) {
trimmedCommand := strings.Trim(strings.ToLower(string(commandStr)), trimSymbols)
parts := strings.Split(trimmedCommand, " ")
// filter empty entries
filteredParts := make([]string, 0, len(parts))
for _, part := range parts {
if len(part) > 0 {
filteredParts = append(filteredParts, part)
}
}
if len(filteredParts) == 0 {
return nil, errors.New("The command was empty")
}
mentions := make([]string, 0, 10)
// parse the command name
var commandType = CommandNone
var commandTarget = CommandTargetNone
for _, part := range filteredParts {
part = strings.Trim(part, trimSymbols)
switch part {
case "start":
commandType = CommandStart
case "stop":
commandType = CommandStop
case "status":
commandType = CommandStatus
case "fortune":
commandType = CommandFortune
// command targets
case "pr":
commandTarget = CommandTargetPR
case "mtflow":
commandTarget = CommandTargetMtFlow
default:
if len(part) > 1 && part[0] == '@' {
mentions = append(mentions, part)
}
}
}
return &Command{Mentions: mentions, Type: commandType, Target: commandTarget, ThreadId: threadId}, nil
}