From 4020364ce13ebea8e1367d576d0fb3ae5b522e6e Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Tue, 28 Jan 2020 15:35:27 +0100 Subject: [PATCH] Fix TaskRuns custom output Custom output like -o name or -o yaml was broken, let's fix it properly --- pkg/cmd/taskrun/describe.go | 39 +++++++++++++++++++++++++++++++- pkg/cmd/taskrun/describe_test.go | 36 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/taskrun/describe.go b/pkg/cmd/taskrun/describe.go index d718ac974..4f79676bf 100644 --- a/pkg/cmd/taskrun/describe.go +++ b/pkg/cmd/taskrun/describe.go @@ -16,6 +16,8 @@ package taskrun import ( "fmt" + "io" + "os" "sort" "text/tabwriter" "text/template" @@ -24,9 +26,11 @@ import ( "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/formatted" validate "github.com/tektoncd/cli/pkg/helper/validate" + "github.com/tektoncd/cli/pkg/printer" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" ) @@ -135,11 +139,20 @@ or Out: cmd.OutOrStdout(), Err: cmd.OutOrStderr(), } - if err := validate.NamespaceExists(p); err != nil { return err } + output, err := cmd.LocalFlags().GetString("output") + if err != nil { + fmt.Fprint(os.Stderr, "Error: output option not set properly \n") + return err + } + + if output != "" { + return describeTaskRunOutput(cmd.OutOrStdout(), p, f, args[0]) + } + return printTaskRunDescription(s, args[0], p) }, } @@ -150,6 +163,30 @@ or return c } +func describeTaskRunOutput(w io.Writer, p cli.Params, f *cliopts.PrintFlags, name string) error { + cs, err := p.Clients() + if err != nil { + return err + } + + c := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()) + + taskrun, err := c.Get(name, metav1.GetOptions{}) + if err != nil { + return err + } + + // NOTE: this is required for -o json|yaml to work properly since + // tektoncd go client fails to set these; probably a bug + taskrun.GetObjectKind().SetGroupVersionKind( + schema.GroupVersionKind{ + Version: "tekton.dev/v1alpha1", + Kind: "TaskRun", + }) + + return printer.PrintObject(w, taskrun, f) +} + func printTaskRunDescription(s *cli.Stream, trName string, p cli.Params) error { cs, err := p.Clients() if err != nil { diff --git a/pkg/cmd/taskrun/describe_test.go b/pkg/cmd/taskrun/describe_test.go index 687f4acf3..ffd57cdc0 100644 --- a/pkg/cmd/taskrun/describe_test.go +++ b/pkg/cmd/taskrun/describe_test.go @@ -16,6 +16,7 @@ package taskrun import ( "fmt" + "strings" "testing" "time" @@ -700,3 +701,38 @@ func Test_SortStepStatesByStartTime_Steps_Terminated_And_Running(t *testing.T) { t.Errorf("sortStepStatesByStartTime should be step3 but returned: %s", element3) } } + +func TestPipelineRunsDescribe_custom_output(t *testing.T) { + name := "task-run" + expected := "taskrun.tekton.dev/" + name + + clock := clockwork.NewFakeClock() + + trs := []*v1alpha1.TaskRun{ + tb.TaskRun(name, "ns"), + } + + cs, _ := test.SeedTestData(t, pipelinetest.Data{ + TaskRuns: trs, + Namespaces: []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "ns", + }, + }, + }, + }) + + p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube} + pipelinerun := Command(p) + + got, err := test.ExecuteCommand(pipelinerun, "desc", "-o", "name", "-n", "ns", name) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + got = strings.TrimSpace(got) + if got != expected { + t.Errorf("Result should be '%s' != '%s'", got, expected) + } +}