diff --git a/pkg/cmd/clustertask/describe.go b/pkg/cmd/clustertask/describe.go index 4dd421b9f..c3c1a7ad8 100644 --- a/pkg/cmd/clustertask/describe.go +++ b/pkg/cmd/clustertask/describe.go @@ -16,6 +16,8 @@ package clustertask import ( "fmt" + "io" + "os" "sort" "text/tabwriter" "text/template" @@ -24,8 +26,10 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/formatted" + "github.com/tektoncd/cli/pkg/printer" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" ) @@ -134,6 +138,16 @@ or Err: cmd.OutOrStderr(), } + 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 describeClusterTaskOutput(cmd.OutOrStdout(), p, f, args[0]) + } + return printClusterTaskDescription(s, p, args[0]) }, } @@ -143,6 +157,30 @@ or return c } +func describeClusterTaskOutput(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().ClusterTasks() + + clustertask, 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 + clustertask.GetObjectKind().SetGroupVersionKind( + schema.GroupVersionKind{ + Version: "tekton.dev/v1alpha1", + Kind: "ClusterTask", + }) + + return printer.PrintObject(w, clustertask, f) +} + func printClusterTaskDescription(s *cli.Stream, p cli.Params, tname string) error { cs, err := p.Clients() if err != nil { diff --git a/pkg/cmd/clustertask/describe_test.go b/pkg/cmd/clustertask/describe_test.go index 80d1e19b2..7137aadfd 100644 --- a/pkg/cmd/clustertask/describe_test.go +++ b/pkg/cmd/clustertask/describe_test.go @@ -215,3 +215,38 @@ func Test_ClusterTaskDescribe(t *testing.T) { }) } } + +func TestClusterTask_custom_output(t *testing.T) { + name := "clustertask" + expected := "clustertask.tekton.dev/" + name + + clock := clockwork.NewFakeClock() + + cstasks := []*v1alpha1.ClusterTask{ + tb.ClusterTask(name), + } + + cs, _ := test.SeedTestData(t, pipelinetest.Data{ + ClusterTasks: cstasks, + Namespaces: []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "", + }, + }, + }, + }) + + p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube} + clustertask := Command(p) + + got, err := test.ExecuteCommand(clustertask, "desc", "-o", "name", 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) + } +}