From 063685f023081e2e238a8c38aeeddb8f248c1681 Mon Sep 17 00:00:00 2001 From: Sarasa Kisaragi Date: Fri, 22 Sep 2023 22:04:42 +0800 Subject: [PATCH] chore: update output for diff (#60) --- cmd/sync.go | 8 ++++++-- pkg/data/events.go | 20 ++++++++++++++++---- pkg/data/events_test.go | 6 +++--- test/cli/suites-basic/diff.go | 10 +++++----- test/cli/suites-consumer-group/diff.go | 6 +++--- test/cli/suites-consumer/diff.go | 4 ++-- test/cli/suites-global-rule/diff.go | 4 ++-- test/cli/suites-plugin-config/diff.go | 4 ++-- test/cli/suites-plugin-metadata/diff.go | 4 ++-- test/mtls/suites-basic/diff.go | 10 +++++----- 10 files changed, 46 insertions(+), 30 deletions(-) diff --git a/cmd/sync.go b/cmd/sync.go index a562d97..80d0513 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -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 @@ -108,7 +108,11 @@ func sync(cmd *cobra.Command, dryRun bool) error { } } - color.Green("Summary: created %d, updated %d, deleted %d", summary.created, summary.updated, summary.deleted) + if dryRun { + color.Green("Summary: create %d, update %d, delete %d", summary.created, summary.updated, summary.deleted) + } else { + color.Green("Summary: created %d, updated %d, deleted %d", summary.created, summary.updated, summary.deleted) + } return nil } diff --git a/pkg/data/events.go b/pkg/data/events.go index 56b377b..fbb5d0f 100644 --- a/pkg/data/events.go +++ b/pkg/data/events.go @@ -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 { @@ -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("update %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 diff --git a/pkg/data/events_test.go b/pkg/data/events_test.go index 27c0ef1..699110d 100644 --- a/pkg/data/events_test.go +++ b/pkg/data/events_test.go @@ -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) @@ -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) @@ -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") diff --git a/test/cli/suites-basic/diff.go b/test/cli/suites-basic/diff.go index 6c9e342..3788605 100644 --- a/test/cli/suites-basic/diff.go +++ b/test/cli/suites-basic/diff.go @@ -13,11 +13,11 @@ 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" -Summary: created 4, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ service: "svc1" ++++ service: "svc2" ++++ route: "route1" ++++ route: "route2" +Summary: create 4, update 0, delete 0 `)) }) }) diff --git a/test/cli/suites-consumer-group/diff.go b/test/cli/suites-consumer-group/diff.go index 3ac0bbf..66e0d47 100644 --- a/test/cli/suites-consumer-group/diff.go +++ b/test/cli/suites-consumer-group/diff.go @@ -13,9 +13,9 @@ 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" -Summary: created 2, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ consumer_group: "company_a" ++++ consumer: "jack" +Summary: create 2, update 0, delete 0 `)) }) }) diff --git a/test/cli/suites-consumer/diff.go b/test/cli/suites-consumer/diff.go index 790d4af..c8a16ed 100644 --- a/test/cli/suites-consumer/diff.go +++ b/test/cli/suites-consumer/diff.go @@ -13,8 +13,8 @@ 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" -Summary: created 1, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ consumer: "jack" +Summary: create 1, update 0, delete 0 `)) }) }) diff --git a/test/cli/suites-global-rule/diff.go b/test/cli/suites-global-rule/diff.go index b7ae32f..60a3a5c 100644 --- a/test/cli/suites-global-rule/diff.go +++ b/test/cli/suites-global-rule/diff.go @@ -13,8 +13,8 @@ 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" -Summary: created 1, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ global_rule: "1" +Summary: create 1, update 0, delete 0 `)) }) }) diff --git a/test/cli/suites-plugin-config/diff.go b/test/cli/suites-plugin-config/diff.go index feebf61..52080ba 100644 --- a/test/cli/suites-plugin-config/diff.go +++ b/test/cli/suites-plugin-config/diff.go @@ -13,8 +13,8 @@ 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" -Summary: created 1, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ plugin_config: "1" +Summary: create 1, update 0, delete 0 `)) }) }) diff --git a/test/cli/suites-plugin-metadata/diff.go b/test/cli/suites-plugin-metadata/diff.go index 1f3e284..34882d8 100644 --- a/test/cli/suites-plugin-metadata/diff.go +++ b/test/cli/suites-plugin-metadata/diff.go @@ -13,8 +13,8 @@ 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" -Summary: created 1, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ plugin_metadata: "http-logger" +Summary: create 1, update 0, delete 0 `)) }) }) diff --git a/test/mtls/suites-basic/diff.go b/test/mtls/suites-basic/diff.go index f1a6c92..09fcb84 100644 --- a/test/mtls/suites-basic/diff.go +++ b/test/mtls/suites-basic/diff.go @@ -13,11 +13,11 @@ 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" -Summary: created 4, updated 0, deleted 0 + gomega.Expect(out).To(gomega.Equal(`+++ service: "svc1" ++++ service: "svc2" ++++ route: "route1" ++++ route: "route2" +Summary: create 4, update 0, delete 0 `)) }) })