Skip to content

Commit d1be2f1

Browse files
committed
diff: prettify premature diff log
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
1 parent dab658f commit d1be2f1

File tree

4 files changed

+408
-9
lines changed

4 files changed

+408
-9
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/fluxcd/pkg/ssa v0.24.1
1515
github.com/fluxcd/source-controller/api v0.35.1
1616
github.com/go-logr/logr v1.2.3
17+
github.com/google/go-cmp v0.5.9
1718
github.com/hashicorp/go-retryablehttp v0.7.2
1819
github.com/onsi/gomega v1.26.0
1920
github.com/spf13/pflag v1.0.5
@@ -80,7 +81,6 @@ require (
8081
github.com/golang/protobuf v1.5.2 // indirect
8182
github.com/google/btree v1.1.2 // indirect
8283
github.com/google/gnostic v0.6.9 // indirect
83-
github.com/google/go-cmp v0.5.9 // indirect
8484
github.com/google/gofuzz v1.2.0 // indirect
8585
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8686
github.com/google/uuid v1.3.0 // indirect

internal/cmp/simple_unstructured.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Copyright 2023 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmp
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
"strings"
23+
24+
"github.com/google/go-cmp/cmp"
25+
)
26+
27+
// SimpleUnstructuredReporter is a simple reporter for Unstructured objects
28+
// that only records differences detected during comparison, in a diff-like
29+
// format.
30+
type SimpleUnstructuredReporter struct {
31+
path cmp.Path
32+
diffs []string
33+
}
34+
35+
// Report writes a diff entry if rs is not equal. In the format of:
36+
//
37+
// spec.replicas
38+
// -3
39+
// +1
40+
//
41+
// spec.template.spec.containers.[0].command.[6]
42+
// ---deleted=true
43+
//
44+
// spec.template.spec.containers.[0].env.[?->1]:
45+
// +map[name:ADDED]
46+
func (r *SimpleUnstructuredReporter) Report(rs cmp.Result) {
47+
if !rs.Equal() {
48+
vx, vy := r.path.Last().Values()
49+
isNonEmptyX := isValidAndNonEmpty(vx)
50+
isNonEmptyY := isValidAndNonEmpty(vy)
51+
52+
if !isNonEmptyX && !isNonEmptyY {
53+
// Skip empty values.
54+
return
55+
}
56+
57+
var sb strings.Builder
58+
sb.WriteString(pathToString(r.path))
59+
sb.WriteString("\n")
60+
if isNonEmptyX {
61+
sb.WriteString(fmt.Sprintf("-%v", vx))
62+
sb.WriteString("\n")
63+
}
64+
if isNonEmptyY {
65+
sb.WriteString(fmt.Sprintf("+%v", vy))
66+
sb.WriteString("\n")
67+
}
68+
r.diffs = append(r.diffs, sb.String())
69+
}
70+
}
71+
72+
// String returns the diff entries joined together with newline, trimmed from
73+
// spaces.
74+
func (r *SimpleUnstructuredReporter) String() string {
75+
return strings.TrimSpace(strings.Join(r.diffs, "\n"))
76+
}
77+
78+
func (r *SimpleUnstructuredReporter) PushStep(ps cmp.PathStep) {
79+
r.path = append(r.path, ps)
80+
}
81+
82+
func (r *SimpleUnstructuredReporter) PopStep() {
83+
r.path = r.path[:len(r.path)-1]
84+
}
85+
86+
func isValidAndNonEmpty(v reflect.Value) bool {
87+
if !v.IsValid() {
88+
return false
89+
}
90+
switch v.Kind() {
91+
case reflect.Interface:
92+
return isValidAndNonEmpty(v.Elem())
93+
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
94+
return v.Len() > 0
95+
}
96+
return true
97+
}
98+
99+
func pathToString(path cmp.Path) (p string) {
100+
for _, step := range path {
101+
switch t := step.(type) {
102+
case cmp.MapIndex:
103+
p += fmt.Sprintf(".%v", t.Key())
104+
case cmp.SliceIndex:
105+
p += fmt.Sprintf("%v", t.String())
106+
}
107+
}
108+
p = strings.TrimPrefix(p, ".")
109+
return
110+
}

0 commit comments

Comments
 (0)