Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement output for pipeline describe #641

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}