-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse_helm_diff.go
143 lines (127 loc) · 3.15 KB
/
collapse_helm_diff.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
134
135
136
137
138
139
140
141
142
143
package main
import (
"bufio"
"flag"
"io"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
const (
COLOR_RESET = "\x1b[0m"
RESOURCE_DIFF = "\x1b[0;33m"
ELLIPSIS = "..."
)
var DIFF_COLOR_PREFIXES = []string{"\x1b[0;31m-", "\x1b[0;32m+"}
func matchesPattern(str string, patterns []*regexp.Regexp) bool {
for _, pattern := range patterns {
if pattern.MatchString(str) {
return true
}
}
return false
}
func coloredWith(line string, prefix string) bool {
return strings.HasPrefix(line, prefix) && strings.HasSuffix(line, COLOR_RESET)
}
func diffLine(line string) bool {
for _, color := range DIFF_COLOR_PREFIXES {
if coloredWith(line, color) {
return true
}
}
return false
}
func uncolor(line string) string {
for _, color := range DIFF_COLOR_PREFIXES {
if coloredWith(line, color) {
return strings.TrimSuffix(strings.TrimPrefix(line, color), COLOR_RESET)
}
}
return line
}
func echo(w io.Writer, str string) {
cmd := exec.Command("echo", "-e", str)
cmd.Stdout = w
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func collapse(w io.Writer, lines []string, patterns []*regexp.Regexp) {
if len(lines) == 0 {
return
}
header, body := lines[0], lines[1:]
if matchesPattern(uncolor(header), patterns) {
header = ELLIPSIS
body = lines
}
echoCollapsed(w, header, body)
}
func processResourceDiff(w io.Writer, diffs []string, patterns []*regexp.Regexp) {
if len(diffs) == 0 {
return
}
diffHeader, lines := diffs[0], diffs[1:]
toBeCollapsed := []string{diffHeader}
for _, line := range lines {
if diffLine(line) {
if matchesPattern(uncolor(line), patterns) {
toBeCollapsed = append(toBeCollapsed, line)
} else {
//TODO: Keep more context (one line before?, what if this line matchesPattern)
//if len(toBeCollapsed) != 0 {
// contextLine, rest := toBeCollapsed[len(toBeCollapsed)-1], toBeCollapsed[:len(toBeCollapsed)-1]
// collapse(rest)
// echo(contextLine)
//}
collapse(w, toBeCollapsed, patterns)
toBeCollapsed = []string{}
echo(w, line)
}
} else {
toBeCollapsed = append(toBeCollapsed, line)
}
}
collapse(w, toBeCollapsed, patterns)
}
func collapseHelmDiff(input *bufio.Scanner, output io.Writer, args []string) {
flag := flag.NewFlagSet(COLLAPSE_HELM_DIFF_CMD_NAME, flag.ExitOnError)
err := flag.Parse(args)
if err != nil {
log.Fatal(err)
}
regexes := flag.Args()
if len(regexes) == 0 {
log.Fatal("No regex was provided. Usage: command regex1 regex2 ...")
}
patterns := make([]*regexp.Regexp, len(regexes))
for i, regex := range regexes {
pattern := regexp.MustCompile(regex)
patterns[i] = pattern
}
resourceDiff := []string{}
for input.Scan() {
rawLine := input.Text()
if coloredWith(rawLine, RESOURCE_DIFF) {
processResourceDiff(output, resourceDiff, patterns)
resourceDiff = []string{}
}
resourceDiff = append(resourceDiff, rawLine)
}
processResourceDiff(output, resourceDiff, patterns)
if err := input.Err(); err != nil {
log.Fatal(err)
}
}
func CollapseHelmDiff(args []string) {
// read stdin
input := bufio.NewScanner(os.Stdin)
// write to stdout
output := os.Stdout
collapseHelmDiff(input, output, args)
}