-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
git_town_output_parser.go
85 lines (71 loc) · 2.15 KB
/
git_town_output_parser.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
package output
import (
"strings"
)
// ExecutedGitCommand describes a Git command that was executed by Git Town during testing.
type ExecutedGitCommand struct {
// Branch contains the branch in which this command ran.
Branch string
// Command contains the command executed.
Command string
// frontend or backend
CommandType CommandType
}
type CommandType struct {
name string
}
var (
CommandTypeFrontend = CommandType{"frontend"} //nolint:gochecknoglobals
CommandTypeBackend = CommandType{"backend"} //nolint:gochecknoglobals
)
func (self CommandType) String() string { return self.name }
// GitCommandsInGitTownOutput provides the Git commands mentioned in the given Git Town output.
func GitCommandsInGitTownOutput(output string) []ExecutedGitCommand {
result := []ExecutedGitCommand{}
for _, line := range strings.Split(output, "\n") {
if lineContainsFrontendCommand(line) {
line := parseFrontendLine(line)
if line != nil {
result = append(result, *line)
}
} else if lineContainsBackendCommand(line) {
result = append(result, parseBackendLine(line))
}
}
return result
}
const backendCommandLineBeginning = "(verbose) " // "\e[1m"
func lineContainsBackendCommand(line string) bool {
return strings.HasPrefix(line, backendCommandLineBeginning)
}
const frontendCommandLineBeginning = "\x1b[1m" // "\e[1m"
func lineContainsFrontendCommand(line string) bool {
return strings.HasPrefix(line, frontendCommandLineBeginning)
}
func parseBackendLine(line string) ExecutedGitCommand {
command := strings.TrimPrefix(line, backendCommandLineBeginning)
return ExecutedGitCommand{
Command: command,
CommandType: CommandTypeBackend,
Branch: "",
}
}
func parseFrontendLine(line string) *ExecutedGitCommand {
line = strings.TrimPrefix(line, frontendCommandLineBeginning)
if line == "" {
return nil
}
// extract branch name if it exists
branch := ""
if line[0] == '[' {
// line contains a branch name
closingParent := strings.IndexRune(line, ']')
branch = line[1:closingParent]
line = line[closingParent+2:]
}
return &ExecutedGitCommand{
Command: line,
CommandType: CommandTypeFrontend,
Branch: branch,
}
}