diff --git a/gnmidiff/README.md b/gnmidiff/README.md index e616d154b..64a2031e9 100644 --- a/gnmidiff/README.md +++ b/gnmidiff/README.md @@ -6,8 +6,8 @@ $ go install ./gnmidiff $ gnmidiff setrequest cmd/demo/setrequest.textproto cmd/demo/setrequest2.textproto SetRequestIntentDiff(-A, +B): --------- deletes -------- -+ /network-instances/network-instance[name=VrfBlue]: deleted +-------- deletes/replaces -------- ++ /network-instances/network-instance[name=VrfBlue]: deleted or replaced only in B -------- updates -------- m /system/config/hostname: - "violetsareblue" diff --git a/gnmidiff/diff.go b/gnmidiff/diff.go index 009f3c0e8..2a24bc393 100644 --- a/gnmidiff/diff.go +++ b/gnmidiff/diff.go @@ -92,6 +92,12 @@ type Format struct { aName string // bName is an optional custom name for B in the diff. bName string + // deleteTitle is an optional custom name when titling the diff section + // with delete paths. + deleteTitle string + // deleteDesc is an optional custom description when describing a + // delete path in the diff. + deleteDesc string } func formatJSONValue(value interface{}) interface{} { @@ -115,11 +121,17 @@ func (diff StructuredDiff) Format(f Format) string { if f.bName == "" { f.bName = "B" } + if f.deleteTitle == "" { + f.deleteDesc = "deletes" + } + if f.deleteDesc == "" { + f.deleteTitle = "deleted only in %s" + } b.WriteString(fmt.Sprintf("%s(-%s, +%s):\n", f.title, f.aName, f.bName)) deleteDiff := diff.DeleteDiff.format(f) if deleteDiff != "" { - b.WriteString("-------- deletes --------\n") + b.WriteString(fmt.Sprintf("-------- %s --------\n", f.deleteTitle)) b.WriteString(deleteDiff) b.WriteString("-------- updates --------\n") } @@ -177,7 +189,15 @@ func (diff DeleteDiff) format(f Format) string { } sort.Strings(paths) for _, path := range paths { - b.WriteString(fmt.Sprintf("%c %s: deleted\n", symbol, path)) + b.WriteString(fmt.Sprintf("%c %s: "+f.deleteDesc, symbol, path)) + switch symbol { + case '-': + b.WriteString(fmt.Sprintf(" only in %s\n", f.aName)) + case '+': + b.WriteString(fmt.Sprintf(" only in %s\n", f.bName)) + default: + b.WriteString("\n") + } } } diff --git a/gnmidiff/setrequest.go b/gnmidiff/setrequest.go index 90661c98e..deb13f205 100644 --- a/gnmidiff/setrequest.go +++ b/gnmidiff/setrequest.go @@ -37,6 +37,8 @@ func (diff SetRequestIntentDiff) Format(f Format) string { f.title = "SetRequestIntentDiff" f.aName = "A" f.bName = "B" + f.deleteTitle = "deletes/replaces" + f.deleteDesc = "deleted or replaced" return StructuredDiff(diff).Format(f) } diff --git a/gnmidiff/setrequest_test.go b/gnmidiff/setrequest_test.go index 26b91beef..9e5d34dec 100644 --- a/gnmidiff/setrequest_test.go +++ b/gnmidiff/setrequest_test.go @@ -80,9 +80,9 @@ func TestSetRequestDiffFormat(t *testing.T) { }, inFormat: Format{}, want: `SetRequestIntentDiff(-A, +B): --------- deletes -------- -- /interfaces/interface[name=eth1]: deleted -+ /interfaces/interface[name=eth2]: deleted +-------- deletes/replaces -------- +- /interfaces/interface[name=eth1]: deleted or replaced only in A ++ /interfaces/interface[name=eth2]: deleted or replaced only in B -------- updates -------- - /interfaces/interface[name=eth1]/config/name: "eth1" - /interfaces/interface[name=eth1]/name: "eth1" @@ -146,10 +146,10 @@ m /interfaces/interface[name=eth0]/subinterfaces/subinterface[index=0]/state/nam Full: true, }, want: `SetRequestIntentDiff(-A, +B): --------- deletes -------- - /interfaces/interface[name=eth0]: deleted -- /interfaces/interface[name=eth1]: deleted -+ /interfaces/interface[name=eth2]: deleted +-------- deletes/replaces -------- + /interfaces/interface[name=eth0]: deleted or replaced +- /interfaces/interface[name=eth1]: deleted or replaced only in A ++ /interfaces/interface[name=eth2]: deleted or replaced only in B -------- updates -------- /interfaces/interface[name=eth0]/config/description: "I am an eth port" /interfaces/interface[name=eth0]/config/name: "eth0"