Skip to content

Commit

Permalink
chore: update output for diff (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel authored Sep 22, 2023
1 parent 5d60893 commit 063685f
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 30 deletions.
8 changes: 6 additions & 2 deletions 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 All @@ -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
}
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("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
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
10 changes: 5 additions & 5 deletions test/cli/suites-basic/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/cli/suites-consumer-group/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/cli/suites-consumer/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/cli/suites-global-rule/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/cli/suites-plugin-config/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/cli/suites-plugin-metadata/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down
10 changes: 5 additions & 5 deletions test/mtls/suites-basic/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down

0 comments on commit 063685f

Please sign in to comment.