From a63f45b6fa8209a5383c3677b9f23029dc46a467 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Sun, 26 Jan 2020 20:11:23 +0100 Subject: [PATCH] Implement output for pipeline describe Closes: #581, #640 Signed-off-by: Chmouel Boudjnah --- pkg/cmd/pipeline/describe.go | 45 +++++++++++++++++++++++++++++++ pkg/cmd/pipeline/describe_test.go | 34 +++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/pkg/cmd/pipeline/describe.go b/pkg/cmd/pipeline/describe.go index 65412db4f..52185a14c 100644 --- a/pkg/cmd/pipeline/describe.go +++ b/pkg/cmd/pipeline/describe.go @@ -17,6 +17,7 @@ package pipeline import ( "fmt" "io" + "os" "sort" "strings" "text/tabwriter" @@ -26,8 +27,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" + "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" ) @@ -101,6 +105,16 @@ func describeCommand(p cli.Params) *cobra.Command { 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 describePipelineOutput(cmd.OutOrStdout(), p, f, args[0]) + } + return printPipelineDescription(cmd.OutOrStdout(), p, args[0]) }, } @@ -110,6 +124,37 @@ func describeCommand(p cli.Params) *cobra.Command { return c } +func describePipelineOutput(w io.Writer, p cli.Params, f *cliopts.PrintFlags, pname 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) +} + +func outputPipeline(cs versioned.Interface, ns, pname string) (*v1alpha1.Pipeline, error) { + c := cs.TektonV1alpha1().Pipelines(ns) + + pipeline, err := c.Get(pname, metav1.GetOptions{}) + if err != nil { + return nil, 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( + schema.GroupVersionKind{ + Version: "tekton.dev/v1alpha1", + Kind: "Pipeline", + }) + return pipeline, nil +} + func printPipelineDescription(out io.Writer, p cli.Params, pname string) error { cs, err := p.Clients() if err != nil { diff --git a/pkg/cmd/pipeline/describe_test.go b/pkg/cmd/pipeline/describe_test.go index 3efed1026..70d89542a 100644 --- a/pkg/cmd/pipeline/describe_test.go +++ b/pkg/cmd/pipeline/describe_test.go @@ -16,6 +16,7 @@ package pipeline import ( "fmt" + "strings" "testing" "time" @@ -331,3 +332,36 @@ func TestPipelinesDescribe_with_multiple_resource_param_task_run(t *testing.T) { } golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } + +func TestPipelinesDescribe_custom_output(t *testing.T) { + clock := clockwork.NewFakeClock() + cs, _ := test.SeedTestData(t, pipelinetest.Data{ + Pipelines: []*v1alpha1.Pipeline{ + tb.Pipeline("pipeline", "ns", + tb.PipelineSpec( + tb.PipelineDeclaredResource("name", v1alpha1.PipelineResourceTypeGit), + ), + ), + }, + Namespaces: []*corev1.Namespace{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "ns", + }, + }, + }, + }) + + p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube} + pipeline := Command(p) + + got, err := test.ExecuteCommand(pipeline, "desc", "-o", "name", "-n", "ns", "pipeline") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + got = strings.TrimSpace(got) + if got != "pipeline.tekton.dev/pipeline" { + t.Errorf("Result should be 'pipeline.tekton.dev/pipeline' != '%s'", got) + } +}