Skip to content

Commit 063685f

Browse files
authored
chore: update output for diff (#60)
1 parent 5d60893 commit 063685f

File tree

10 files changed

+46
-30
lines changed

10 files changed

+46
-30
lines changed

cmd/sync.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func sync(cmd *cobra.Command, dryRun bool) error {
8282
summary.deleted++
8383
}
8484

85-
str, err := event.Output()
85+
str, err := event.Output(dryRun)
8686
if err != nil {
8787
color.Red("Failed to get output of the event: %v", err)
8888
return err
@@ -108,7 +108,11 @@ func sync(cmd *cobra.Command, dryRun bool) error {
108108
}
109109
}
110110

111-
color.Green("Summary: created %d, updated %d, deleted %d", summary.created, summary.updated, summary.deleted)
111+
if dryRun {
112+
color.Green("Summary: create %d, update %d, delete %d", summary.created, summary.updated, summary.deleted)
113+
} else {
114+
color.Green("Summary: created %d, updated %d, deleted %d", summary.created, summary.updated, summary.deleted)
115+
}
112116

113117
return nil
114118
}

pkg/data/events.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,21 @@ type Event struct {
5757
// if the event is create, it will return the message of creating resource.
5858
// if the event is update, it will return the diff of old value and new value.
5959
// if the event is delete, it will return the message of deleting resource.
60-
func (e *Event) Output() (string, error) {
60+
func (e *Event) Output(diffOnly bool) (string, error) {
6161
var output string
6262
switch e.Option {
6363
case CreateOption:
64-
output = fmt.Sprintf("creating %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.Value))
64+
if diffOnly {
65+
output = fmt.Sprintf("+++ %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.Value))
66+
} else {
67+
output = fmt.Sprintf("creating %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.Value))
68+
}
6569
case DeleteOption:
66-
output = fmt.Sprintf("deleting %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.OldValue))
70+
if diffOnly {
71+
output = fmt.Sprintf("--- %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.OldValue))
72+
} else {
73+
output = fmt.Sprintf("deleting %s: \"%s\"", e.ResourceType, apisix.GetResourceUniqueKey(e.OldValue))
74+
}
6775
case UpdateOption:
6876
remote, err := json.MarshalIndent(e.OldValue, "", "\t")
6977
if err != nil {
@@ -79,7 +87,11 @@ func (e *Event) Output() (string, error) {
7987

8088
edits := myers.ComputeEdits(span.URIFromPath("remote"), string(remote), string(local))
8189
diff := fmt.Sprint(gotextdiff.ToUnified("remote", "local", string(remote), edits))
82-
output = fmt.Sprintf("updating %s: \"%s\"\n%s", e.ResourceType, apisix.GetResourceUniqueKey(e.Value), diff)
90+
if diffOnly {
91+
output = fmt.Sprintf("update %s: \"%s\"\n%s", e.ResourceType, apisix.GetResourceUniqueKey(e.Value), diff)
92+
} else {
93+
output = fmt.Sprintf("updating %s: \"%s\"\n%s", e.ResourceType, apisix.GetResourceUniqueKey(e.Value), diff)
94+
}
8395
}
8496

8597
return output, nil

pkg/data/events_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestEventOutput(t *testing.T) {
5050
Option: DeleteOption,
5151
OldValue: svc,
5252
}
53-
output, err := event.Output()
53+
output, err := event.Output(false)
5454
assert.Nil(t, err, "should not return error")
5555
assert.Equal(t, "deleting service: \"svc\"", output)
5656

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

@@ -73,7 +73,7 @@ func TestEventOutput(t *testing.T) {
7373
OldValue: route,
7474
Value: route1,
7575
}
76-
output, err = event.Output()
76+
output, err = event.Output(false)
7777
assert.Nil(t, err, "should not return error")
7878
assert.Contains(t, output, "updating route: \"route\"", "should contain the route name")
7979
assert.Contains(t, output, "+\t\"desc\": \"route1\"", "should contain the changes")

test/cli/suites-basic/diff.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ var _ = ginkgo.Describe("`adc diff` tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-basic/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating service: "svc1"
17-
creating service: "svc2"
18-
creating route: "route1"
19-
creating route: "route2"
20-
Summary: created 4, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ service: "svc1"
17+
+++ service: "svc2"
18+
+++ route: "route1"
19+
+++ route: "route2"
20+
Summary: create 4, update 0, delete 0
2121
`))
2222
})
2323
})

test/cli/suites-consumer-group/diff.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ var _ = ginkgo.Describe("`adc diff` consumer group tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-consumer-group/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating consumer_group: "company_a"
17-
creating consumer: "jack"
18-
Summary: created 2, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ consumer_group: "company_a"
17+
+++ consumer: "jack"
18+
Summary: create 2, update 0, delete 0
1919
`))
2020
})
2121
})

test/cli/suites-consumer/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var _ = ginkgo.Describe("`adc diff` consumer tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-consumer/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating consumer: "jack"
17-
Summary: created 1, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ consumer: "jack"
17+
Summary: create 1, update 0, delete 0
1818
`))
1919
})
2020
})

test/cli/suites-global-rule/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var _ = ginkgo.Describe("`adc diff` global rule tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-global-rule/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating global_rule: "1"
17-
Summary: created 1, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ global_rule: "1"
17+
Summary: create 1, update 0, delete 0
1818
`))
1919
})
2020
})

test/cli/suites-plugin-config/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var _ = ginkgo.Describe("`adc diff` plugin config tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-plugin-config/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating plugin_config: "1"
17-
Summary: created 1, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ plugin_config: "1"
17+
Summary: create 1, update 0, delete 0
1818
`))
1919
})
2020
})

test/cli/suites-plugin-metadata/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var _ = ginkgo.Describe("`adc diff` plugin metadata tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-plugin-metadata/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating plugin_metadata: "http-logger"
17-
Summary: created 1, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ plugin_metadata: "http-logger"
17+
Summary: create 1, update 0, delete 0
1818
`))
1919
})
2020
})

test/mtls/suites-basic/diff.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ var _ = ginkgo.Describe("`adc diff` tests", func() {
1313
ginkgo.It("should return the diff result", func() {
1414
out, err := s.Diff("suites-basic/testdata/test.yaml")
1515
gomega.Expect(err).To(gomega.BeNil())
16-
gomega.Expect(out).To(gomega.Equal(`creating service: "svc1"
17-
creating service: "svc2"
18-
creating route: "route1"
19-
creating route: "route2"
20-
Summary: created 4, updated 0, deleted 0
16+
gomega.Expect(out).To(gomega.Equal(`+++ service: "svc1"
17+
+++ service: "svc2"
18+
+++ route: "route1"
19+
+++ route: "route2"
20+
Summary: create 4, update 0, delete 0
2121
`))
2222
})
2323
})

0 commit comments

Comments
 (0)