Skip to content

Commit

Permalink
Add machine readable output (-o flag) to kn source binding describe
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaustubh-pande committed Nov 27, 2020
1 parent 394f4be commit bcbdda9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
| https://github.com/knative/client/pull/1150[#1150]

| 🎁
| Add machine readable output (-o flag) to kn source binding describe
| https://github.com/knative/client/pull/1148[#1148]

| 🐛
| Add machine readable output (-o flag) to kn source apiserver describe
| https://github.com/knative/client/pull/1146[#1146]

Expand Down
14 changes: 10 additions & 4 deletions docs/cmd/kn_source_binding_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ kn source binding describe NAME

```
# Describe a sink binding with name 'mysinkbinding'
# Describe a sink binding 'mysinkbinding'
kn source binding describe mysinkbinding
# Describe a sink binding 'mysinkbinding' in YAML format
kn source binding describe mysinkbinding -o yaml
```

### Options

```
-h, --help help for describe
-n, --namespace string Specify the namespace to operate in.
-v, --verbose More output.
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for describe
-n, --namespace string Specify the namespace to operate in.
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
-v, --verbose More output.
```

### Options inherited from parent commands
Expand Down
39 changes: 30 additions & 9 deletions pkg/kn/commands/source/binding/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"errors"
"fmt"
"sort"
"strings"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
"knative.dev/pkg/tracker"

Expand All @@ -28,14 +30,23 @@ import (
"knative.dev/client/pkg/printers"
)

var describeExample = `
# Describe a sink binding 'mysinkbinding'
kn source binding describe mysinkbinding
# Describe a sink binding 'mysinkbinding' in YAML format
kn source binding describe mysinkbinding -o yaml`

// NewBindingDescribeCommand returns a new command for describe a sink binding object
func NewBindingDescribeCommand(p *commands.KnParams) *cobra.Command {
cmd := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a sink binding",
Example: `
# Describe a sink binding with name 'mysinkbinding'
kn source binding describe mysinkbinding`,

// For machine readable output
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

command := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a sink binding",
Example: describeExample,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn source binding describe' requires name of the sink binding as single argument")
Expand All @@ -55,6 +66,15 @@ func NewBindingDescribeCommand(p *commands.KnParams) *cobra.Command {
out := cmd.OutOrStdout()
dw := printers.NewPrefixWriter(out)

// Print out machine readable output if requested
if machineReadablePrintFlags.OutputFlagSpecified() {
printer, err := machineReadablePrintFlags.ToPrinter()
if err != nil {
return err
}
return printer.PrintObj(binding, out)
}

printDetails, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
Expand All @@ -75,11 +95,12 @@ func NewBindingDescribeCommand(p *commands.KnParams) *cobra.Command {
return nil
},
}
flags := cmd.Flags()
flags := command.Flags()
commands.AddNamespaceFlags(flags, false)
flags.BoolP("verbose", "v", false, "More output.")

return cmd
machineReadablePrintFlags.AddFlags(command)
command.Flag("output").Usage = fmt.Sprintf("Output format. One of: %s.", strings.Join(machineReadablePrintFlags.AllowedFormats(), "|"))
return command
}

func writeSinkBinding(dw printers.PrefixWriter, binding *v1alpha2.SinkBinding, printDetails bool) {
Expand Down
17 changes: 16 additions & 1 deletion pkg/kn/commands/source/binding/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,24 @@ func TestDescribeWithSinkURI(t *testing.T) {
bindingRecorder.Validate()
}

func TestSinkBindingMachineReadableOutputs(t *testing.T) {
bindingClient := clientv1alpha2.NewMockKnSinkBindingClient(t, "mynamespace")
bindingRecorder := bindingClient.Recorder()

bindingRecorder.GetSinkBinding("mybinding", getSinkBindingSource("myapp", map[string]string{"foo": "bar"}, createServiceSink("mysvc", "myservicenamespace")), nil)
out, err := executeSinkBindingCommand(bindingClient, nil, "describe", "mybinding", "-o", "yaml")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "kind: SinkBinding", "spec:", "status:", "metadata:"))

bindingRecorder.Validate()
}

func getSinkBindingSource(nameOrSelector string, ceOverrides map[string]string, sink duckv1.Destination) *v1alpha2.SinkBinding {
binding := &v1alpha2.SinkBinding{
TypeMeta: v1.TypeMeta{},
TypeMeta: v1.TypeMeta{
Kind: "SinkBinding",
APIVersion: "sources.knative.dev/v1alpha2",
},
ObjectMeta: v1.ObjectMeta{
Name: "mysinkbinding",
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/sources/v1alpha2/binding_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (c *knBindingClient) GetSinkBinding(name string) (*v1alpha2.SinkBinding, er
if err != nil {
return nil, knerrors.GetError(err)
}
err = updateSinkBindingGvk(binding)
if err != nil {
return nil, err
}
return binding, nil
}

Expand Down

0 comments on commit bcbdda9

Please sign in to comment.