-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from n3wscott/ce-diff
add cloudevent diff command
- Loading branch information
Showing
34 changed files
with
1,568 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
module github.com/cloudevents/conformance | ||
|
||
go 1.14 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/spf13/cobra v0.0.5 | ||
github.com/google/go-cmp v0.5.7 | ||
github.com/kylelemons/godebug v1.1.0 | ||
github.com/spf13/cobra v1.3.0 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2022 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package commands | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/cloudevents/conformance/pkg/commands/options" | ||
"github.com/cloudevents/conformance/pkg/diff" | ||
) | ||
|
||
func addDiff(topLevel *cobra.Command) { | ||
do := &options.DiffOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "diff file_a file_b", | ||
Short: "Compare differences between two sets of event data.", | ||
Example: ` | ||
cloudevents diff ./want.yaml ./got.yaml | ||
`, | ||
Args: cobra.ExactArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
r := diff.Diff{ | ||
Out: cmd.OutOrStdout(), | ||
FileA: args[0], | ||
FileB: args[1], | ||
FindBy: do.FindBy, | ||
FullDiff: do.FullDiff, | ||
IgnoreAdditions: do.IgnoreAdditions, | ||
} | ||
if err := r.Do(); err != nil { | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
do.AddFlags(cmd) | ||
|
||
topLevel.AddCommand(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright 2022 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package options | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// DiffOptions | ||
type DiffOptions struct { | ||
FindBy []string | ||
|
||
FullDiff bool | ||
IgnoreAdditions bool | ||
} | ||
|
||
func (o *DiffOptions) AddFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringArrayVar(&o.FindBy, "match", []string{"id"}, | ||
"Find by keys to compare each event set.") | ||
|
||
cmd.Flags().BoolVar(&o.FullDiff, "full", false, | ||
"Print the full diff.") | ||
|
||
cmd.Flags().BoolVar(&o.IgnoreAdditions, "ignore-additions", false, | ||
"Ignore additions between file_a and file_b.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
Copyright 2022 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package diff | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/cloudevents/conformance/pkg/event" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
cmpdiff "github.com/kylelemons/godebug/diff" | ||
) | ||
|
||
// Diff compares two CloudEvents yaml files (or directories) for differences, | ||
// ignoring `Mode` and `TransportExtensions`. | ||
type Diff struct { | ||
Out io.Writer | ||
|
||
FindBy []string | ||
FullDiff bool | ||
IgnoreAdditions bool | ||
|
||
FileA string | ||
FileB string | ||
} | ||
|
||
func (i *Diff) find(like event.Event, all []event.Event) (string, *event.Event) { | ||
label := "" | ||
for _, b := range i.FindBy { | ||
if len(label) == 0 { | ||
label = fmt.Sprintf("%s[%s]", b, like.Get(b)) | ||
} else { | ||
label = fmt.Sprintf("%s %s[%s]", label, b, like.Get(b)) | ||
} | ||
} | ||
|
||
// Look for the matching ID, Source and Subject. | ||
for _, a := range all { | ||
match := true | ||
for _, key := range i.FindBy { | ||
if a.Get(key) != like.Get(key) { | ||
match = false | ||
break | ||
} | ||
} | ||
if match { | ||
return label, &a | ||
} | ||
} | ||
return label, nil | ||
} | ||
|
||
func (i *Diff) Do() error { | ||
ignoreOpts := cmpopts.IgnoreFields(event.Event{}, "Mode", "TransportExtensions") | ||
|
||
eventsA, err := event.FromYaml(i.FileA, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
eventsB, err := event.FromYaml(i.FileB, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var diffs []string | ||
|
||
for _, a := range eventsA { | ||
label, b := i.find(a, eventsB) | ||
|
||
if b == nil { | ||
_, _ = fmt.Fprintf(i.Out, "missing: %s\n", label) | ||
diffs = append(diffs, fmt.Sprintf("missing: %s\n", label)) | ||
continue | ||
} | ||
|
||
if diff := cmp.Diff(a, b, ignoreOpts); diff != "" { | ||
// Clear out Mode and Transport Extensions. | ||
a.Mode = "" | ||
b.Mode = "" | ||
a.TransportExtensions = nil | ||
b.TransportExtensions = nil | ||
|
||
ab, err := event.ToYaml(a) | ||
if err != nil { | ||
return err | ||
} | ||
bb, err := event.ToYaml(*b) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ignore := true | ||
sb := &strings.Builder{} | ||
if len(diffs) >= 1 { | ||
sb.WriteString("---\n") | ||
} | ||
sb.WriteString(fmt.Sprintf("%s diffs (-a, +b):\n", label)) | ||
|
||
chunks := cmpdiff.DiffChunks(strings.Split(string(ab), "\n"), strings.Split(string(bb), "\n")) | ||
changed := map[string]bool{} | ||
for _, c := range chunks { | ||
if len(c.Added) != 0 { | ||
for _, a := range c.Added { | ||
if _, found := changed[strings.Split(a, ":")[0]]; found || !i.IgnoreAdditions { | ||
ignore = false | ||
sb.WriteString(fmt.Sprintf("+ %s\n", a)) | ||
} | ||
} | ||
} | ||
if len(c.Deleted) != 0 { | ||
ignore = false | ||
for _, d := range c.Deleted { | ||
sb.WriteString(fmt.Sprintf("- %s\n", d)) | ||
changed[strings.Split(d, ":")[0]] = true | ||
} | ||
} | ||
} | ||
|
||
if i.FullDiff { | ||
_, _ = fmt.Fprintf(i.Out, "%s\n", cmpdiff.Diff(string(ab), string(bb))) | ||
} else if !ignore { | ||
_, _ = fmt.Fprint(i.Out, sb.String()) | ||
if len(diffs) > 0 { | ||
diffs = append(diffs, "---") | ||
} | ||
diffs = append(diffs, cmpdiff.Diff(string(ab), string(bb))) | ||
} | ||
} | ||
} | ||
|
||
if len(diffs) > 0 { | ||
return errors.New(strings.Join(diffs, "\n")) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.