-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.go
More file actions
60 lines (46 loc) · 1.33 KB
/
formatter.go
File metadata and controls
60 lines (46 loc) · 1.33 KB
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
package utils //nolint:revive // This is a utility package.
import (
"bytes"
"regexp"
"strings"
)
const keyValueParts = 2
// splitOutput splits the output into blocks based on the regular expression.
// TODO add tests.
func SplitOutput(regularExpression *regexp.Regexp, output []byte) [][]byte {
indices := regularExpression.FindAllIndex(output, -1)
if indices == nil {
return nil // No matches found
}
var blocks [][]byte
start := 0
for i, match := range indices {
if i == 0 {
continue // Skip the first match
}
block := output[start:match[0]] // everything before the match
if len(block) > 0 { // avoid empty blocks
blocks = append(blocks, bytes.TrimSpace(block)) // trim space here
}
start = match[0] // Start of the next block is the current match
}
// Add the last block if any
if start < len(output) {
blocks = append(blocks, bytes.TrimSpace(output[start:]))
}
return blocks
}
// FIXME Might go in another file
// ParseLineDetail parses a line of the show detail command and returns the key and value.
func ParseLineDetail(line string) (key, value string) {
if line == "" {
return "", ""
}
splitParts := strings.Split(line, ":")
if len(splitParts) != keyValueParts {
return "", ""
}
key = strings.TrimSpace(splitParts[0])
value = strings.TrimSpace(splitParts[1])
return key, value
}