Skip to content

Commit

Permalink
uniform multiple writeSink into DescribeSink
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Oct 27, 2020
1 parent abb75e4 commit e54a9f4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 68 deletions.
3 changes: 3 additions & 0 deletions lib/printing/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func DescribeSink(dw printers.PrefixWriter, attribute, namespace string, sink *d
if sink == nil {
return
}
if len(attribute) == 0 {
attribute = "Sink"
}
subWriter := dw.WriteAttribute(attribute, "")
ref := sink.Ref
if ref != nil {
Expand Down
16 changes: 16 additions & 0 deletions pkg/kn/commands/flags/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

clientdynamic "knative.dev/client/pkg/dynamic"
"knative.dev/client/pkg/kn/config"
"knative.dev/client/pkg/printers"
)

type SinkFlags struct {
Expand Down Expand Up @@ -147,3 +148,18 @@ func SinkToString(sink duckv1.Destination) string {
}
return ""
}

// WriteSink writes a sink to a PrefixWriter which is used in describe command
func WriteSink(dw printers.PrefixWriter, sink *duckv1.Destination) {
subWriter := dw.WriteAttribute("Sink", "")
ref := sink.Ref
if ref != nil {
subWriter.WriteAttribute("Name", sink.Ref.Name)
subWriter.WriteAttribute("Namespace", sink.Ref.Namespace)
subWriter.WriteAttribute("Resource", fmt.Sprintf("%s (%s)", sink.Ref.Kind, sink.Ref.APIVersion))
}
uri := sink.URI
if uri != nil {
subWriter.WriteAttribute("URI", uri.String())
}
}
18 changes: 2 additions & 16 deletions pkg/kn/commands/source/apiserver/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (

"github.com/spf13/cobra"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/client/lib/printing"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/printers"
)
Expand Down Expand Up @@ -65,7 +65,7 @@ func NewAPIServerDescribeCommand(p *commands.KnParams) *cobra.Command {
return err
}

writeSink(dw, apiSource.Spec.Sink)
printing.DescribeSink(dw, "", apiSource.Namespace, &apiSource.Spec.Sink)
dw.WriteLine()
if err := dw.Flush(); err != nil {
return err
Expand Down Expand Up @@ -107,20 +107,6 @@ func writeResources(dw printers.PrefixWriter, apiVersionKindSelectors []v1alpha2
}
}

func writeSink(dw printers.PrefixWriter, sink duckv1.Destination) {
subWriter := dw.WriteAttribute("Sink", "")
ref := sink.Ref
if ref != nil {
subWriter.WriteAttribute("Name", sink.Ref.Name)
subWriter.WriteAttribute("Namespace", sink.Ref.Namespace)
subWriter.WriteAttribute("Kind", fmt.Sprintf("%s (%s)", sink.Ref.Kind, sink.Ref.APIVersion))
}
uri := sink.URI
if uri != nil {
subWriter.WriteAttribute("URI", uri.String())
}
}

func writeAPIServerSource(dw printers.PrefixWriter, source *v1alpha2.ApiServerSource, printDetails bool) {
commands.WriteMetadata(dw, &source.ObjectMeta, printDetails)
dw.WriteAttribute("ServiceAccountName", source.Spec.ServiceAccountName)
Expand Down
20 changes: 2 additions & 18 deletions pkg/kn/commands/source/binding/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

"github.com/spf13/cobra"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/tracker"

"knative.dev/client/lib/printing"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/printers"
)
Expand Down Expand Up @@ -85,28 +85,12 @@ func NewBindingDescribeCommand(p *commands.KnParams) *cobra.Command {
func writeSinkBinding(dw printers.PrefixWriter, binding *v1alpha2.SinkBinding, printDetails bool) {
commands.WriteMetadata(dw, &binding.ObjectMeta, printDetails)
writeSubject(dw, binding.Namespace, &binding.Spec.Subject)
writeSink(dw, binding.Namespace, &binding.Spec.Sink)
printing.DescribeSink(dw, "", binding.Namespace, &binding.Spec.Sink)
if binding.Spec.CloudEventOverrides != nil && binding.Spec.CloudEventOverrides.Extensions != nil {
writeCeOverrides(dw, binding.Spec.CloudEventOverrides.Extensions)
}
}

func writeSink(dw printers.PrefixWriter, namespace string, sink *duckv1.Destination) {
subWriter := dw.WriteAttribute("Sink", "")
ref := sink.Ref
if ref != nil {
subWriter.WriteAttribute("Name", sink.Ref.Name)
if sink.Ref.Namespace != "" && sink.Ref.Namespace != namespace {
subWriter.WriteAttribute("Namespace", sink.Ref.Namespace)
}
subWriter.WriteAttribute("Resource", fmt.Sprintf("%s (%s)", sink.Ref.Kind, sink.Ref.APIVersion))
}
uri := sink.URI
if uri != nil {
subWriter.WriteAttribute("URI", uri.String())
}
}

func writeCeOverrides(dw printers.PrefixWriter, ceOverrides map[string]string) {
subDw := dw.WriteAttribute("CloudEvent Overrides", "")
keys := make([]string, 0, len(ceOverrides))
Expand Down
19 changes: 2 additions & 17 deletions pkg/kn/commands/source/ping/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package ping

import (
"errors"
"fmt"
"sort"

"github.com/spf13/cobra"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/client/lib/printing"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/printers"
)
Expand Down Expand Up @@ -67,7 +66,7 @@ func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
}

// Revisions summary info
writeSink(dw, &pingSource.Spec.Sink)
printing.DescribeSink(dw, "", pingSource.Namespace, &pingSource.Spec.Sink)
dw.WriteLine()
if err := dw.Flush(); err != nil {
return err
Expand Down Expand Up @@ -97,20 +96,6 @@ func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
return pingDescribe
}

func writeSink(dw printers.PrefixWriter, sink *duckv1.Destination) {
subWriter := dw.WriteAttribute("Sink", "")
ref := sink.Ref
if ref != nil {
subWriter.WriteAttribute("Name", sink.Ref.Name)
subWriter.WriteAttribute("Namespace", sink.Ref.Namespace)
subWriter.WriteAttribute("Resource", fmt.Sprintf("%s (%s)", sink.Ref.Kind, sink.Ref.APIVersion))
}
uri := sink.URI
if uri != nil {
subWriter.WriteAttribute("URI", uri.String())
}
}

func writePingSource(dw printers.PrefixWriter, source *v1alpha2.PingSource, printDetails bool) {
commands.WriteMetadata(dw, &source.ObjectMeta, printDetails)
dw.WriteAttribute("Schedule", source.Spec.Schedule)
Expand Down
19 changes: 2 additions & 17 deletions pkg/kn/commands/trigger/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package trigger

import (
"errors"
"fmt"

"github.com/spf13/cobra"

v1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1"
duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/client/lib/printing"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/printers"
)
Expand Down Expand Up @@ -74,7 +73,7 @@ func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command {
}

// Revisions summary info
writeSink(dw, &trigger.Spec.Subscriber)
printing.DescribeSink(dw, "", trigger.Namespace, &trigger.Spec.Subscriber)
dw.WriteLine()
if err := dw.Flush(); err != nil {
return err
Expand All @@ -96,20 +95,6 @@ func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command {
return triggerDescribe
}

func writeSink(dw printers.PrefixWriter, sink *duckv1.Destination) {
subWriter := dw.WriteAttribute("Sink", "")
ref := sink.Ref
if ref != nil {
subWriter.WriteAttribute("Name", sink.Ref.Name)
subWriter.WriteAttribute("Namespace", sink.Ref.Namespace)
subWriter.WriteAttribute("Resource", fmt.Sprintf("%s (%s)", sink.Ref.Kind, sink.Ref.APIVersion))
}
uri := sink.URI
if uri != nil {
subWriter.WriteAttribute("URI", uri.String())
}
}

func writeTrigger(dw printers.PrefixWriter, trigger *v1beta1.Trigger, printDetails bool) {
commands.WriteMetadata(dw, &trigger.ObjectMeta, printDetails)
dw.WriteAttribute("Broker", trigger.Spec.Broker)
Expand Down

0 comments on commit e54a9f4

Please sign in to comment.