legacy_diff.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package diff
  14. import (
  15. "bytes"
  16. "fmt"
  17. "strings"
  18. "text/tabwriter"
  19. "k8s.io/apimachinery/pkg/util/dump"
  20. )
  21. // ObjectGoPrintSideBySide prints a and b as textual dumps side by side,
  22. // enabling easy visual scanning for mismatches.
  23. func ObjectGoPrintSideBySide(a, b interface{}) string {
  24. sA := dump.Pretty(a)
  25. sB := dump.Pretty(b)
  26. linesA := strings.Split(sA, "\n")
  27. linesB := strings.Split(sB, "\n")
  28. width := 0
  29. for _, s := range linesA {
  30. l := len(s)
  31. if l > width {
  32. width = l
  33. }
  34. }
  35. for _, s := range linesB {
  36. l := len(s)
  37. if l > width {
  38. width = l
  39. }
  40. }
  41. buf := &bytes.Buffer{}
  42. w := tabwriter.NewWriter(buf, width, 0, 1, ' ', 0)
  43. max := len(linesA)
  44. if len(linesB) > max {
  45. max = len(linesB)
  46. }
  47. for i := 0; i < max; i++ {
  48. var a, b string
  49. if i < len(linesA) {
  50. a = linesA[i]
  51. }
  52. if i < len(linesB) {
  53. b = linesB[i]
  54. }
  55. _, _ = fmt.Fprintf(w, "%s\t%s\n", a, b)
  56. }
  57. _ = w.Flush()
  58. return buf.String()
  59. }