Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update output for diff #60

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func sync(cmd *cobra.Command, dryRun bool) error {
summary.deleted++
}

str, err := event.Output()
str, err := event.Output(dryRun)
if err != nil {
color.Red("Failed to get output of the event: %v", err)
return err
Expand Down
20 changes: 16 additions & 4 deletions pkg/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ type Event struct {
// if the event is create, it will return the message of creating resource.
// if the event is update, it will return the diff of old value and new value.
// if the event is delete, it will return the message of deleting resource.
func (e *Event) Output() (string, error) {
func (e *Event) Output(diffOnly bool) (string, error) {
var output string
switch e.Option {
case CreateOption:
output = fmt.Sprintf("creating %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.Value))
if diffOnly {
output = fmt.Sprintf("+++ %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.Value))
} else {
output = fmt.Sprintf("creating %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.Value))
}
case DeleteOption:
output = fmt.Sprintf("deleting %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.OldValue))
if diffOnly {
output = fmt.Sprintf("--- %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.OldValue))
} else {
output = fmt.Sprintf("deleting %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.OldValue))
}
case UpdateOption:
remote, err := json.MarshalIndent(e.OldValue, "", "\t")
if err != nil {
Expand All @@ -79,7 +87,11 @@ func (e *Event) Output() (string, error) {

edits := myers.ComputeEdits(span.URIFromPath("remote"), string(remote), string(local))
diff := fmt.Sprint(gotextdiff.ToUnified("remote", "local", string(remote), edits))
output = fmt.Sprintf("updating %s: \"%s\"\n%s", e.ResourceType, apisix.GetResourceUniqueKey(e.Value), diff)
if diffOnly {
output = fmt.Sprintf("diff %s: \"%s\"\n%s", e.ResourceType, apisix.GetResourceUniqueKey(e.Value), diff)
} else {
output = fmt.Sprintf("updating %s: \"%s\"\n%s", e.ResourceType, apisix.GetResourceUniqueKey(e.Value), diff)
}
}

return output, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/data/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestEventOutput(t *testing.T) {
Option: DeleteOption,
OldValue: svc,
}
output, err := event.Output()
output, err := event.Output(false)
assert.Nil(t, err, "should not return error")
assert.Equal(t, "deleting service: \"svc\"", output)

Expand All @@ -60,7 +60,7 @@ func TestEventOutput(t *testing.T) {
Option: CreateOption,
Value: svc,
}
output, err = event.Output()
output, err = event.Output(false)
assert.Nil(t, err, "should not return error")
assert.Equal(t, "creating service: \"svc\"", output)

Expand All @@ -73,7 +73,7 @@ func TestEventOutput(t *testing.T) {
OldValue: route,
Value: route1,
}
output, err = event.Output()
output, err = event.Output(false)
assert.Nil(t, err, "should not return error")
assert.Contains(t, output, "updating route: \"route\"", "should contain the route name")
assert.Contains(t, output, "+\t\"desc\": \"route1\"", "should contain the changes")
Expand Down
8 changes: 4 additions & 4 deletions test/cli/suites-basic/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ var _ = ginkgo.Describe("`adc diff` tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-basic/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating service: "svc1"
creating service: "svc2"
creating route: "route1"
creating route: "route2"
gomega.Expect(out).To(gomega.Equal(`+++ service: "svc1"
+++ service: "svc2"
+++ route: "route1"
+++ route: "route2"
Summary: created 4, updated 0, deleted 0
`))
})
Expand Down
4 changes: 2 additions & 2 deletions test/cli/suites-consumer-group/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var _ = ginkgo.Describe("`adc diff` consumer group tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-consumer-group/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating consumer_group: "company_a"
creating consumer: "jack"
gomega.Expect(out).To(gomega.Equal(`+++ consumer_group: "company_a"
+++ consumer: "jack"
Summary: created 2, updated 0, deleted 0
`))
})
Expand Down
2 changes: 1 addition & 1 deletion test/cli/suites-consumer/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc diff` consumer tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-consumer/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating consumer: "jack"
gomega.Expect(out).To(gomega.Equal(`+++ consumer: "jack"
Summary: created 1, updated 0, deleted 0
`))
})
Expand Down
2 changes: 1 addition & 1 deletion test/cli/suites-global-rule/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc diff` global rule tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-global-rule/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating global_rule: "1"
gomega.Expect(out).To(gomega.Equal(`+++ global_rule: "1"
Summary: created 1, updated 0, deleted 0
`))
})
Expand Down
2 changes: 1 addition & 1 deletion test/cli/suites-plugin-config/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc diff` plugin config tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-plugin-config/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating plugin_config: "1"
gomega.Expect(out).To(gomega.Equal(`+++ plugin_config: "1"
Summary: created 1, updated 0, deleted 0
`))
})
Expand Down
2 changes: 1 addition & 1 deletion test/cli/suites-plugin-metadata/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = ginkgo.Describe("`adc diff` plugin metadata tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-plugin-metadata/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating plugin_metadata: "http-logger"
gomega.Expect(out).To(gomega.Equal(`+++ plugin_metadata: "http-logger"
Summary: created 1, updated 0, deleted 0
`))
})
Expand Down
8 changes: 4 additions & 4 deletions test/mtls/suites-basic/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ var _ = ginkgo.Describe("`adc diff` tests", func() {
ginkgo.It("should return the diff result", func() {
out, err := s.Diff("suites-basic/testdata/test.yaml")
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(out).To(gomega.Equal(`creating service: "svc1"
creating service: "svc2"
creating route: "route1"
creating route: "route2"
gomega.Expect(out).To(gomega.Equal(`+++ service: "svc1"
+++ service: "svc2"
+++ route: "route1"
+++ route: "route2"
Summary: created 4, updated 0, deleted 0
`))
})
Expand Down