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

Fix custom output on describe commands #648

Merged
merged 6 commits into from
Jan 28, 2020
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
38 changes: 38 additions & 0 deletions pkg/cmd/clustertask/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package clustertask

import (
"fmt"
"io"
"os"
"sort"
"text/tabwriter"
"text/template"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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])
},
}
Expand All @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions pkg/cmd/clustertask/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
22 changes: 7 additions & 15 deletions pkg/cmd/pipeline/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 39 additions & 1 deletion pkg/cmd/pipelineresource/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

import (
"fmt"
"io"
"os"
"sort"
"text/tabwriter"
"text/template"
Expand All @@ -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"
)

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

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

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