Skip to content

Commit

Permalink
Implement output for pipeline describe
Browse files Browse the repository at this point in the history
Closes: cloudfoundry#581, cloudfoundry#640

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel authored and tekton-robot committed Jan 27, 2020
1 parent 32ac7b8 commit 0dcf77c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/cmd/pipeline/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pipeline
import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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])
},
}
Expand All @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions pkg/cmd/pipeline/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pipeline

import (
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 0dcf77c

Please sign in to comment.