-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
122 lines (102 loc) · 2.77 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
package main
import (
"errors"
"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 = iota
// CommandStop command stops 'something'
CommandStop = iota
// CommandStatus command gets the status of 'something'
CommandStatus = iota
)
func (commandType CommandType) String() string {
switch commandType {
case CommandNone:
return "none"
case CommandStart:
return "start"
case CommandStop:
return "stop"
case CommandStatus:
return "status"
default:
panic("CommandType switch not considering all cases!")
}
}
// 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 = iota
// CommandTargetMtFlow is this program :)
CommandTargetMtFlow = iota
)
func (commandTarget CommandTarget) String() string {
switch commandTarget {
case CommandTargetNone:
return "none"
case CommandTargetPR:
return "pr"
case CommandTargetMtFlow:
return "mtflow"
default:
panic("CommandTarget switch not considering all cases!")
}
}
// Command is the metadata about the type, target, and whom the
// command was issued to
type Command struct {
Type CommandType
Target CommandTarget
Mentions []string
}
// ParseCommand takes care of turning a text string into a proper command
func ParseCommand(commandStr 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
// 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}, nil
}