Skip to content

Commit

Permalink
feat: colorized diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
dervoeti committed Nov 5, 2024
1 parent e1f0782 commit 08e1a98
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,19 @@ func countLines(k string, v interface{}) (int, error) {
return strings.Count(buf.String(), "\n"), nil
}

// We did not find a good library to create colorized diffs, so we do it ourselves the cheap way
func colorizeDiff(diff string) string {
diffLines := strings.Split(diff, "\n")
for i, line := range diffLines {
if strings.HasPrefix(line, "+") {
diffLines[i] = "\033[32m" + line + "\033[0m"
} else if strings.HasPrefix(line, "-") {
diffLines[i] = "\033[31m" + line + "\033[0m"
}
}
return strings.Join(diffLines, "\n")
}

// PrettyDiff creates a unified diff highlighting the differences between two Kubernetes resources
func PrettyDiff(expected *unstructured.Unstructured, actual *unstructured.Unstructured) (string, error) {
actualPruned := pruneLargeAdditions(expected, actual)
Expand All @@ -450,7 +463,8 @@ func PrettyDiff(expected *unstructured.Unstructured, actual *unstructured.Unstru
Context: 3,
}

return difflib.GetUnifiedDiffString(diffed)
diffStr, err := difflib.GetUnifiedDiffString(diffed)
return "\n" + colorizeDiff(diffStr), err
}

// ConvertUnstructured converts an unstructured object to the known struct. If the type is not known, then
Expand Down

0 comments on commit 08e1a98

Please sign in to comment.