Skip to content

Commit

Permalink
Add machine readable output (-o flag) to kn source ping describe
Browse files Browse the repository at this point in the history
Signed-off-by: Arghya Sadhu <[email protected]>
  • Loading branch information
arghya88 committed Nov 27, 2020
1 parent b8bce53 commit 435cf7c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
|===
| | Description | PR

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

| 🐛
| 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_ping_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ kn source ping describe NAME

```
# Describe a Ping source with name 'myping'
# Describe a ping source with name 'myping'
kn source ping describe myping
# Describe a ping source with name 'myping' in YAML format
kn source ping describe myping -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
40 changes: 31 additions & 9 deletions pkg/kn/commands/source/ping/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ package ping

import (
"errors"
"fmt"
"sort"
"strings"

"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/spf13/cobra"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
Expand All @@ -26,15 +30,23 @@ import (
"knative.dev/client/pkg/printers"
)

var describeExample = `
# Describe a ping source with name 'myping'
kn source ping describe myping
# Describe a ping source with name 'myping' in YAML format
kn source ping describe myping -o yaml`

// NewPingDescribeCommand returns a new command for describe a Ping source object
func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {

pingDescribe := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a ping source",
Example: `
# Describe a Ping source with name 'myping'
kn source ping describe myping`,
// For machine readable output
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

command := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a ping source",
Example: describeExample,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn source ping describe' requires name of the source as single argument")
Expand All @@ -52,6 +64,15 @@ func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
}

out := cmd.OutOrStdout()

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

printDetails, err := cmd.Flags().GetBool("verbose")
Expand Down Expand Up @@ -89,11 +110,12 @@ func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
return nil
},
}
flags := pingDescribe.Flags()
flags := command.Flags()
commands.AddNamespaceFlags(flags, false)
flags.BoolP("verbose", "v", false, "More output.")

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

func writePingSource(dw printers.PrefixWriter, source *v1alpha2.PingSource, printDetails bool) {
Expand Down
17 changes: 16 additions & 1 deletion pkg/kn/commands/source/ping/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ func TestDescribeURI(t *testing.T) {
pingRecorder.Validate()
}

func TestDescribeMachineReadable(t *testing.T) {
pingClient := clientv1alpha2.NewMockKnPingSourceClient(t, "mynamespace")

pingRecorder := pingClient.Recorder()
pingRecorder.GetPingSource("testsource-uri", getPingSourceSinkURI(), nil)

out, err := executePingSourceCommand(pingClient, nil, "describe", "testsource-uri", "-o", "yaml")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "kind: PingSource", "spec:", "status:", "metadata:"))
pingRecorder.Validate()
}

func TestDescribeError(t *testing.T) {
pingClient := clientv1alpha2.NewMockKnPingSourceClient(t, "mynamespace")

Expand All @@ -71,7 +83,10 @@ func TestDescribeError(t *testing.T) {

func getPingSourceSinkURI() *v1alpha2.PingSource {
return &v1alpha2.PingSource{
TypeMeta: metav1.TypeMeta{},
TypeMeta: metav1.TypeMeta{
Kind: "PingSource",
APIVersion: "sources.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testsource-uri",
Namespace: "mynamespace",
Expand Down
10 changes: 9 additions & 1 deletion pkg/sources/v1alpha2/ping_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ func (c *pingSourcesClient) DeletePingSource(name string) error {
}

func (c *pingSourcesClient) GetPingSource(name string) (*v1alpha2.PingSource, error) {
return c.client.Get(context.TODO(), name, metav1.GetOptions{})
source, err := c.client.Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = updateSourceGVK(source)
if err != nil {
return nil, err
}
return source, nil
}

// ListPingSource returns the available Ping sources
Expand Down

0 comments on commit 435cf7c

Please sign in to comment.