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

Add --no-headers flag to tkn clustertask list command #1244

Merged
merged 1 commit into from
Nov 16, 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
1 change: 1 addition & 0 deletions docs/cmd/tkn_clustertask_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Lists ClusterTasks
```
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for list
--no-headers do not print column headers with output (default print column headers with output)
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
```
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-clustertask-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Lists ClusterTasks
\fB\-h\fP, \fB\-\-help\fP[=false]
help for list

.PP
\fB\-\-no\-headers\fP[=false]
do not print column headers with output (default print column headers with output)

.PP
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.
Expand Down
18 changes: 13 additions & 5 deletions pkg/cmd/clustertask/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ import (

const (
emptyMsg = "No ClusterTasks found\n"
header = "NAME\tDESCRIPTION\tAGE"
pratikjagrut marked this conversation as resolved.
Show resolved Hide resolved
body = "%s\t%s\t%s\n"
header = "NAME\tDESCRIPTION\tAGE"
)

type listOptions struct {
NoHeaders bool
}

func listCommand(p cli.Params) *cobra.Command {
opts := &listOptions{}
f := cliopts.NewPrintFlags("list")

c := &cobra.Command{
Expand All @@ -59,15 +64,15 @@ func listCommand(p cli.Params) *cobra.Command {
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}
return printClusterTaskDetails(stream, p)
return printClusterTaskDetails(stream, p, opts.NoHeaders)
},
}
f.AddFlags(c)

c.Flags().BoolVar(&opts.NoHeaders, "no-headers", opts.NoHeaders, "do not print column headers with output (default print column headers with output)")
return c
}

func printClusterTaskDetails(s *cli.Stream, p cli.Params) error {
func printClusterTaskDetails(s *cli.Stream, p cli.Params, noHeaders bool) error {
cs, err := p.Clients()
if err != nil {
return err
Expand All @@ -84,7 +89,10 @@ func printClusterTaskDetails(s *cli.Stream, p cli.Params) error {
}

w := tabwriter.NewWriter(s.Out, 0, 5, 3, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, header)

if !noHeaders {
fmt.Fprintln(w, header)
}

for _, clustertask := range clustertasks.Items {
fmt.Fprintf(w, body,
Expand Down
80 changes: 80 additions & 0 deletions pkg/cmd/clustertask/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,83 @@ func TestClusterTaskListOnlyClusterTasksv1beta1(t *testing.T) {
}
golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name()))
}

func TestClusterTaskListNoHeadersv1alpha1(t *testing.T) {
clock := clockwork.NewFakeClock()

clustertasks := []*v1alpha1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "guavas",
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-1 * time.Minute)},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "avocados",
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-20 * time.Second)},
},
},
}

version := "v1alpha1"
tdc := testDynamic.Options{}
dynamic, err := tdc.Client(
cb.UnstructuredCT(clustertasks[0], version),
cb.UnstructuredCT(clustertasks[1], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

cs, _ := test.SeedTestData(t, pipelinetest.Data{ClusterTasks: clustertasks})
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube, Clock: clock, Dynamic: dynamic}
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"clustertask"})

clustertask := Command(p)
output, err := test.ExecuteCommand(clustertask, "list", "--no-headers")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name()))
}

func TestClusterTaskListNoHeadersv1beta1(t *testing.T) {
clock := clockwork.NewFakeClock()

clustertasks := []*v1beta1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "guavas",
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-1 * time.Minute)},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "avocados",
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-20 * time.Second)},
},
},
}

version := "v1beta1"
tdc := testDynamic.Options{}
dynamic, err := tdc.Client(
cb.UnstructuredV1beta1CT(clustertasks[0], version),
cb.UnstructuredV1beta1CT(clustertasks[1], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

cs, _ := test.SeedV1beta1TestData(t, pipelinev1beta1test.Data{ClusterTasks: clustertasks})
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube, Clock: clock, Dynamic: dynamic}
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"clustertask"})

clustertask := Command(p)
output, err := test.ExecuteCommand(clustertask, "list", "--no-headers")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name()))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
guavas 1 minute ago
avocados 20 seconds ago
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
guavas 1 minute ago
avocados 20 seconds ago