Skip to content

Commit

Permalink
Fix ClusterTask custom output
Browse files Browse the repository at this point in the history
Custom output like -o name or -o yaml was broken, let's fix it properly
  • Loading branch information
chmouel authored and tekton-robot committed Jan 28, 2020
1 parent 4020364 commit 3cfdc0e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
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)
}
}

0 comments on commit 3cfdc0e

Please sign in to comment.