From 25812a7b896dbee1a5a70825c6dbc6c9a8ef6824 Mon Sep 17 00:00:00 2001 From: Arghya Sadhu Date: Fri, 13 Nov 2020 20:27:16 +0530 Subject: [PATCH] add machine readable output for trigger Signed-off-by: Arghya Sadhu --- CHANGELOG.adoc | 5 +++ docs/cmd/kn_trigger_describe.md | 12 +++++-- pkg/eventing/v1beta1/client.go | 4 +++ pkg/kn/commands/trigger/describe.go | 41 ++++++++++++++++++------ pkg/kn/commands/trigger/describe_test.go | 14 ++++++++ 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index e673fcd887..fac6a51b92 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -27,6 +27,11 @@ | https://github.com/knative/client/pull/1118[#1118] |=== +| 🎁 +| Add machine readable output (-o flag) to kn trigger describe +| https://github.com/knative/client/pull/1117[#1121] +|=== + ### v0.19.0 (2020-11-11) [cols="1,10,3", options="header", width="100%"] |=== diff --git a/docs/cmd/kn_trigger_describe.md b/docs/cmd/kn_trigger_describe.md index 6727ef5c3b..5185d9bce2 100644 --- a/docs/cmd/kn_trigger_describe.md +++ b/docs/cmd/kn_trigger_describe.md @@ -16,14 +16,20 @@ kn trigger describe NAME # Describe a trigger with name 'my-trigger' kn trigger describe my-trigger + + # Describe a trigger 'my-trigger' in YAML format + kn trigger describe my-trigger -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 diff --git a/pkg/eventing/v1beta1/client.go b/pkg/eventing/v1beta1/client.go index 92e056ffab..de15c06808 100644 --- a/pkg/eventing/v1beta1/client.go +++ b/pkg/eventing/v1beta1/client.go @@ -97,6 +97,10 @@ func (c *knEventingClient) GetTrigger(name string) (*v1beta1.Trigger, error) { if err != nil { return nil, kn_errors.GetError(err) } + err = updateEventingGVK(trigger) + if err != nil { + return nil, err + } return trigger, nil } diff --git a/pkg/kn/commands/trigger/describe.go b/pkg/kn/commands/trigger/describe.go index 4db9b5cf57..a607c6e0d8 100644 --- a/pkg/kn/commands/trigger/describe.go +++ b/pkg/kn/commands/trigger/describe.go @@ -16,6 +16,10 @@ package trigger import ( "errors" + "fmt" + "strings" + + "k8s.io/cli-runtime/pkg/genericclioptions" "github.com/spf13/cobra" @@ -26,15 +30,23 @@ import ( "knative.dev/client/pkg/printers" ) +var describeExample = ` + # Describe a trigger with name 'my-trigger' + kn trigger describe my-trigger + + # Describe a trigger 'my-trigger' in YAML format + kn trigger describe my-trigger -o yaml` + // NewTriggerDescribeCommand returns a new command for describe a trigger func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command { - triggerDescribe := &cobra.Command{ - Use: "describe NAME", - Short: "Show details of a trigger", - Example: ` - # Describe a trigger with name 'my-trigger' - kn trigger describe my-trigger`, + // For machine readable output + machineReadablePrintFlags := genericclioptions.NewPrintFlags("") + + command := &cobra.Command{ + Use: "describe NAME", + Short: "Show details of a trigger", + Example: describeExample, RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { return errors.New("'kn trigger describe' requires name of the trigger as single argument") @@ -59,6 +71,16 @@ func NewTriggerDescribeCommand(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(trigger, out) + } + dw := printers.NewPrefixWriter(out) printDetails, err := cmd.Flags().GetBool("verbose") @@ -88,11 +110,12 @@ func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command { return nil }, } - flags := triggerDescribe.Flags() + flags := command.Flags() commands.AddNamespaceFlags(flags, false) flags.BoolP("verbose", "v", false, "More output.") - - return triggerDescribe + machineReadablePrintFlags.AddFlags(command) + command.Flag("output").Usage = fmt.Sprintf("Output format. One of: %s.", strings.Join(machineReadablePrintFlags.AllowedFormats(), "|")) + return command } func writeTrigger(dw printers.PrefixWriter, trigger *v1beta1.Trigger, printDetails bool) { diff --git a/pkg/kn/commands/trigger/describe_test.go b/pkg/kn/commands/trigger/describe_test.go index 6e70afea23..a29928e844 100644 --- a/pkg/kn/commands/trigger/describe_test.go +++ b/pkg/kn/commands/trigger/describe_test.go @@ -81,6 +81,20 @@ func TestDescribeTriggerWithSinkURI(t *testing.T) { recorder.Validate() } +func TestDescribeTriggerMachineReadable(t *testing.T) { + client := clientv1beta1.NewMockKnEventingClient(t, "mynamespace") + + recorder := client.Recorder() + recorder.GetTrigger("testtrigger", getTriggerSinkRef(), nil) + + output, err := executeTriggerCommand(client, nil, "describe", "testtrigger", "-o", "yaml") + assert.NilError(t, err) + assert.Assert(t, util.ContainsAll(output, "kind: Trigger", "spec:", "status:", "metadata:")) + + // Validate that all recorded API methods have been called + recorder.Validate() +} + func getTriggerSinkRef() *v1beta1.Trigger { return &v1beta1.Trigger{ TypeMeta: v1.TypeMeta{},