Add machine readable output (-o flag) to kn source ping describe (#1150)

Signed-off-by: Arghya Sadhu <arghya88@gmail.com>
This commit is contained in:
Arghya Sadhu 2020-11-27 18:00:35 +05:30 committed by GitHub
parent f108673aa4
commit 394f4be206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 17 deletions

View File

@ -17,7 +17,11 @@
|=== |===
| | Description | PR | | 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 | Add machine readable output (-o flag) to kn source apiserver describe
| https://github.com/knative/client/pull/1146[#1146] | https://github.com/knative/client/pull/1146[#1146]

View File

@ -14,16 +14,22 @@ kn source ping describe NAME
``` ```
# Describe a Ping source with name 'myping' # Describe a ping source 'myping'
kn source ping describe myping kn source ping describe myping
# Describe a ping source 'myping' in YAML format
kn source ping describe myping -o yaml
``` ```
### Options ### Options
``` ```
-h, --help help for describe --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)
-n, --namespace string Specify the namespace to operate in. -h, --help help for describe
-v, --verbose More output. -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 ### Options inherited from parent commands

View File

@ -16,25 +16,36 @@ package ping
import ( import (
"errors" "errors"
"fmt"
"sort" "sort"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" "k8s.io/cli-runtime/pkg/genericclioptions"
"knative.dev/eventing/pkg/apis/sources/v1alpha2"
"knative.dev/client/lib/printing" "knative.dev/client/lib/printing"
"knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/printers" "knative.dev/client/pkg/printers"
) )
var describeExample = `
# Describe a ping source 'myping'
kn source ping describe myping
# Describe a ping source 'myping' in YAML format
kn source ping describe myping -o yaml`
// NewPingDescribeCommand returns a new command for describe a Ping source object // NewPingDescribeCommand returns a new command for describe a Ping source object
func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command { func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
pingDescribe := &cobra.Command{ // For machine readable output
Use: "describe NAME", machineReadablePrintFlags := genericclioptions.NewPrintFlags("")
Short: "Show details of a ping source",
Example: ` command := &cobra.Command{
# Describe a Ping source with name 'myping' Use: "describe NAME",
kn source ping describe myping`, Short: "Show details of a ping source",
Example: describeExample,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 { if len(args) != 1 {
return errors.New("'kn source ping describe' requires name of the source as single argument") return errors.New("'kn source ping describe' requires name of the source as single argument")
@ -52,6 +63,15 @@ func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
} }
out := cmd.OutOrStdout() 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) dw := printers.NewPrefixWriter(out)
printDetails, err := cmd.Flags().GetBool("verbose") printDetails, err := cmd.Flags().GetBool("verbose")
@ -89,11 +109,12 @@ func NewPingDescribeCommand(p *commands.KnParams) *cobra.Command {
return nil return nil
}, },
} }
flags := pingDescribe.Flags() flags := command.Flags()
commands.AddNamespaceFlags(flags, false) commands.AddNamespaceFlags(flags, false)
flags.BoolP("verbose", "v", false, "More output.") flags.BoolP("verbose", "v", false, "More output.")
machineReadablePrintFlags.AddFlags(command)
return pingDescribe 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) { func writePingSource(dw printers.PrefixWriter, source *v1alpha2.PingSource, printDetails bool) {

View File

@ -55,6 +55,18 @@ func TestDescribeURI(t *testing.T) {
pingRecorder.Validate() 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) { func TestDescribeError(t *testing.T) {
pingClient := clientv1alpha2.NewMockKnPingSourceClient(t, "mynamespace") pingClient := clientv1alpha2.NewMockKnPingSourceClient(t, "mynamespace")
@ -71,7 +83,10 @@ func TestDescribeError(t *testing.T) {
func getPingSourceSinkURI() *v1alpha2.PingSource { func getPingSourceSinkURI() *v1alpha2.PingSource {
return &v1alpha2.PingSource{ return &v1alpha2.PingSource{
TypeMeta: metav1.TypeMeta{}, TypeMeta: metav1.TypeMeta{
Kind: "PingSource",
APIVersion: "sources.knative.dev/v1",
},
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "testsource-uri", Name: "testsource-uri",
Namespace: "mynamespace", Namespace: "mynamespace",

View File

@ -18,6 +18,8 @@ import (
"context" "context"
"fmt" "fmt"
knerrors "knative.dev/client/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/sources/v1alpha2" "knative.dev/eventing/pkg/apis/sources/v1alpha2"
@ -87,7 +89,15 @@ func (c *pingSourcesClient) DeletePingSource(name string) error {
} }
func (c *pingSourcesClient) GetPingSource(name string) (*v1alpha2.PingSource, 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, knerrors.GetError(err)
}
err = updateSourceGVK(source)
if err != nil {
return nil, err
}
return source, nil
} }
// ListPingSource returns the available Ping sources // ListPingSource returns the available Ping sources