Skip to content

Commit

Permalink
diff: prettify premature diff log
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Feb 27, 2023
1 parent dab658f commit d1be2f1
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/fluxcd/pkg/ssa v0.24.1
github.com/fluxcd/source-controller/api v0.35.1
github.com/go-logr/logr v1.2.3
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/onsi/gomega v1.26.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -80,7 +81,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down
110 changes: 110 additions & 0 deletions internal/cmp/simple_unstructured.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2023 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmp

import (
"fmt"
"reflect"
"strings"

"github.com/google/go-cmp/cmp"
)

// SimpleUnstructuredReporter is a simple reporter for Unstructured objects
// that only records differences detected during comparison, in a diff-like
// format.
type SimpleUnstructuredReporter struct {
path cmp.Path
diffs []string
}

// Report writes a diff entry if rs is not equal. In the format of:
//
// spec.replicas
// -3
// +1
//
// spec.template.spec.containers.[0].command.[6]
// ---deleted=true
//
// spec.template.spec.containers.[0].env.[?->1]:
// +map[name:ADDED]
func (r *SimpleUnstructuredReporter) Report(rs cmp.Result) {
if !rs.Equal() {
vx, vy := r.path.Last().Values()
isNonEmptyX := isValidAndNonEmpty(vx)
isNonEmptyY := isValidAndNonEmpty(vy)

if !isNonEmptyX && !isNonEmptyY {
// Skip empty values.
return
}

var sb strings.Builder
sb.WriteString(pathToString(r.path))
sb.WriteString("\n")
if isNonEmptyX {
sb.WriteString(fmt.Sprintf("-%v", vx))
sb.WriteString("\n")
}
if isNonEmptyY {
sb.WriteString(fmt.Sprintf("+%v", vy))
sb.WriteString("\n")
}
r.diffs = append(r.diffs, sb.String())
}
}

// String returns the diff entries joined together with newline, trimmed from
// spaces.
func (r *SimpleUnstructuredReporter) String() string {
return strings.TrimSpace(strings.Join(r.diffs, "\n"))
}

func (r *SimpleUnstructuredReporter) PushStep(ps cmp.PathStep) {
r.path = append(r.path, ps)
}

func (r *SimpleUnstructuredReporter) PopStep() {
r.path = r.path[:len(r.path)-1]
}

func isValidAndNonEmpty(v reflect.Value) bool {
if !v.IsValid() {
return false
}
switch v.Kind() {
case reflect.Interface:
return isValidAndNonEmpty(v.Elem())
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return v.Len() > 0
}
return true
}

func pathToString(path cmp.Path) (p string) {
for _, step := range path {
switch t := step.(type) {
case cmp.MapIndex:
p += fmt.Sprintf(".%v", t.Key())
case cmp.SliceIndex:
p += fmt.Sprintf("%v", t.String())
}
}
p = strings.TrimPrefix(p, ".")
return
}
Loading

0 comments on commit d1be2f1

Please sign in to comment.