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) + } +} diff --git a/pkg/cmd/pipeline/describe.go b/pkg/cmd/pipeline/describe.go index 52185a14c..2b69fbac1 100644 --- a/pkg/cmd/pipeline/describe.go +++ b/pkg/cmd/pipeline/describe.go @@ -29,7 +29,6 @@ import ( validate "github.com/tektoncd/cli/pkg/helper/validate" "github.com/tektoncd/cli/pkg/printer" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" - "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" @@ -124,35 +123,28 @@ func describeCommand(p cli.Params) *cobra.Command { return c } -func describePipelineOutput(w io.Writer, p cli.Params, f *cliopts.PrintFlags, pname string) error { +func describePipelineOutput(w io.Writer, p cli.Params, f *cliopts.PrintFlags, name string) error { cs, err := p.Clients() if err != nil { return err } - pipeline, err := outputPipeline(cs.Tekton, p.Namespace(), pname) - if err != nil { - return err - } - return printer.PrintObject(w, pipeline, f) -} + c := cs.Tekton.TektonV1alpha1().Pipelines(p.Namespace()) -func outputPipeline(cs versioned.Interface, ns, pname string) (*v1alpha1.Pipeline, error) { - c := cs.TektonV1alpha1().Pipelines(ns) - - pipeline, err := c.Get(pname, metav1.GetOptions{}) + task, err := c.Get(name, metav1.GetOptions{}) if err != nil { - return nil, err + return err } // NOTE: this is required for -o json|yaml to work properly since // tektoncd go client fails to set these; probably a bug - pipeline.GetObjectKind().SetGroupVersionKind( + task.GetObjectKind().SetGroupVersionKind( schema.GroupVersionKind{ Version: "tekton.dev/v1alpha1", Kind: "Pipeline", }) - return pipeline, nil + + return printer.PrintObject(w, task, f) } func printPipelineDescription(out io.Writer, p cli.Params, pname string) error { diff --git a/pkg/cmd/pipelineresource/describe.go b/pkg/cmd/pipelineresource/describe.go index cf5dba297..6c8d41b57 100644 --- a/pkg/cmd/pipelineresource/describe.go +++ b/pkg/cmd/pipelineresource/describe.go @@ -16,16 +16,20 @@ package pipelineresource import ( "fmt" + "io" + "os" "text/tabwriter" "text/template" "github.com/tektoncd/cli/pkg/formatted" + validateinput "github.com/tektoncd/cli/pkg/helper/validate" + "github.com/tektoncd/cli/pkg/printer" "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" - validateinput "github.com/tektoncd/cli/pkg/helper/validate" "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" ) @@ -85,6 +89,16 @@ or 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 describePipelineResourceOutput(cmd.OutOrStdout(), p, f, args[0]) + } + return printPipelineResourceDescription(s, p, args[0]) }, } @@ -94,6 +108,30 @@ or return c } +func describePipelineResourceOutput(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().PipelineResources(p.Namespace()) + + pipelineresource, 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 + pipelineresource.GetObjectKind().SetGroupVersionKind( + schema.GroupVersionKind{ + Version: "tekton.dev/v1alpha1", + Kind: "PipelineResource", + }) + + return printer.PrintObject(w, pipelineresource, f) +} + func printPipelineResourceDescription(s *cli.Stream, p cli.Params, preName string) error { cs, err := p.Clients() if err != nil { diff --git a/pkg/cmd/pipelineresource/describe_test.go b/pkg/cmd/pipelineresource/describe_test.go index d4f93786a..ea22dc668 100644 --- a/pkg/cmd/pipelineresource/describe_test.go +++ b/pkg/cmd/pipelineresource/describe_test.go @@ -16,8 +16,10 @@ package pipelineresource import ( "fmt" + "strings" "testing" + "github.com/jonboulle/clockwork" "github.com/tektoncd/cli/pkg/test" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" pipelinetest "github.com/tektoncd/pipeline/test" @@ -120,3 +122,38 @@ func TestPipelineResourceDescribe_WithSecretParams(t *testing.T) { out, _ := test.ExecuteCommand(pipelineresource, "desc", "test-1", "-n", "test-ns-1") golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name())) } + +func TestPipelineResourcesDescribe_custom_output(t *testing.T) { + name := "pipeline-resource" + expected := "pipelineresource.tekton.dev/" + name + + clock := clockwork.NewFakeClock() + + prs := []*v1alpha1.PipelineResource{ + tb.PipelineResource(name, "ns"), + } + + cs, _ := test.SeedTestData(t, pipelinetest.Data{ + PipelineResources: prs, + Namespaces: []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "", + }, + }, + }, + }) + + p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube} + run := Command(p) + + got, err := test.ExecuteCommand(run, "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) + } +} diff --git a/pkg/cmd/pipelinerun/describe.go b/pkg/cmd/pipelinerun/describe.go index 49f161f98..e32dbe830 100644 --- a/pkg/cmd/pipelinerun/describe.go +++ b/pkg/cmd/pipelinerun/describe.go @@ -16,6 +16,8 @@ package pipelinerun 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" ) @@ -119,6 +123,16 @@ or 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 describePiplineRunOutput(cmd.OutOrStdout(), p, f, args[0]) + } + return printPipelineRunDescription(s, args[0], p) }, } @@ -129,6 +143,30 @@ or return c } +func describePiplineRunOutput(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().PipelineRuns(p.Namespace()) + + pipelinerun, 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 + pipelinerun.GetObjectKind().SetGroupVersionKind( + schema.GroupVersionKind{ + Version: "tekton.dev/v1alpha1", + Kind: "PipelineRun", + }) + + return printer.PrintObject(w, pipelinerun, f) +} + func printPipelineRunDescription(s *cli.Stream, prName string, p cli.Params) error { cs, err := p.Clients() if err != nil { diff --git a/pkg/cmd/pipelinerun/describe_test.go b/pkg/cmd/pipelinerun/describe_test.go index 61a5f235a..8f82d173d 100644 --- a/pkg/cmd/pipelinerun/describe_test.go +++ b/pkg/cmd/pipelinerun/describe_test.go @@ -16,6 +16,7 @@ package pipelinerun import ( "fmt" + "strings" "testing" "time" @@ -660,3 +661,36 @@ func TestPipelineRunDescribe_without_tr_start_time(t *testing.T) { } golden.Assert(t, actual, fmt.Sprintf("%s.golden", t.Name())) } + +func TestPipelineRunsDescribe_custom_output(t *testing.T) { + pipelinerunname := "pipeline-run" + expected := "pipelinerun.tekton.dev/" + pipelinerunname + + prun := []*v1alpha1.PipelineRun{ + tb.PipelineRun(pipelinerunname, "ns"), + } + clock := clockwork.NewFakeClock() + cs, _ := test.SeedTestData(t, pipelinetest.Data{ + PipelineRuns: prun, + 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", pipelinerunname) + 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) + } +} diff --git a/pkg/cmd/task/describe.go b/pkg/cmd/task/describe.go index cf176b75b..2887f0ecc 100644 --- a/pkg/cmd/task/describe.go +++ b/pkg/cmd/task/describe.go @@ -16,6 +16,8 @@ package task import ( "fmt" + "io" + "os" "sort" "text/tabwriter" "text/template" @@ -25,8 +27,10 @@ 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" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" cliopts "k8s.io/cli-runtime/pkg/genericclioptions" ) @@ -140,6 +144,16 @@ or 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 describeTaskOutput(cmd.OutOrStdout(), p, f, args[0]) + } + return printTaskDescription(s, p, args[0]) }, } @@ -149,6 +163,30 @@ or return c } +func describeTaskOutput(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().Tasks(p.Namespace()) + + task, 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 + task.GetObjectKind().SetGroupVersionKind( + schema.GroupVersionKind{ + Version: "tekton.dev/v1alpha1", + Kind: "Task", + }) + + return printer.PrintObject(w, task, f) +} + func printTaskDescription(s *cli.Stream, p cli.Params, tname string) error { cs, err := p.Clients() if err != nil { diff --git a/pkg/cmd/task/describe_test.go b/pkg/cmd/task/describe_test.go index 1d4186d82..89c60f001 100644 --- a/pkg/cmd/task/describe_test.go +++ b/pkg/cmd/task/describe_test.go @@ -17,6 +17,7 @@ package task import ( "errors" "fmt" + "strings" "testing" "time" @@ -273,3 +274,37 @@ func TestTaskDescribe_TaskRunError(t *testing.T) { test.AssertOutput(t, expected, out) test.AssertOutput(t, "fake list taskrun error", err.Error()) } + +func TestTaskDescribe_custom_output(t *testing.T) { + name := "task" + expected := "task.tekton.dev/" + name + + clock := clockwork.NewFakeClock() + + tasks := []*v1alpha1.Task{ + tb.Task(name, "ns"), + } + + cs, _ := test.SeedTestData(t, pipelinetest.Data{ + Tasks: tasks, + Namespaces: []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "ns", + }, + }, + }, + }) + + p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube} + task := Command(p) + got, err := test.ExecuteCommand(task, "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) + } +} 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) + } +}