Skip to content

Commit

Permalink
Fix TaskRuns 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 071040d commit 4020364
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
39 changes: 38 additions & 1 deletion pkg/cmd/taskrun/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package taskrun

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 @@ -135,11 +139,20 @@ or
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}

if err := validate.NamespaceExists(p); err != nil {
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 describeTaskRunOutput(cmd.OutOrStdout(), p, f, args[0])
}

return printTaskRunDescription(s, args[0], p)
},
}
Expand All @@ -150,6 +163,30 @@ or
return c
}

func describeTaskRunOutput(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().TaskRuns(p.Namespace())

taskrun, 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
taskrun.GetObjectKind().SetGroupVersionKind(
schema.GroupVersionKind{
Version: "tekton.dev/v1alpha1",
Kind: "TaskRun",
})

return printer.PrintObject(w, taskrun, f)
}

func printTaskRunDescription(s *cli.Stream, trName string, p cli.Params) error {
cs, err := p.Clients()
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions pkg/cmd/taskrun/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package taskrun

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

Expand Down Expand Up @@ -700,3 +701,38 @@ func Test_SortStepStatesByStartTime_Steps_Terminated_And_Running(t *testing.T) {
t.Errorf("sortStepStatesByStartTime should be step3 but returned: %s", element3)
}
}

func TestPipelineRunsDescribe_custom_output(t *testing.T) {
name := "task-run"
expected := "taskrun.tekton.dev/" + name

clock := clockwork.NewFakeClock()

trs := []*v1alpha1.TaskRun{
tb.TaskRun(name, "ns"),
}

cs, _ := test.SeedTestData(t, pipelinetest.Data{
TaskRuns: trs,
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", 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 4020364

Please sign in to comment.