Skip to content

Commit

Permalink
Add flag to reverse sort taskruns/pipelineruns
Browse files Browse the repository at this point in the history
  • Loading branch information
waveywaves authored and tekton-robot committed Mar 10, 2020
1 parent 3b88797 commit 3591a56
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/cmd/tkn_pipelinerun_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ List all PipelineRuns in a namespace 'foo':
--label string A selector (label query) to filter on, supports '=', '==', and '!='
--limit int limit pipelineruns listed (default: return all pipelineruns)
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--reverse list pipelineruns in reverse order
--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
1 change: 1 addition & 0 deletions docs/cmd/tkn_taskrun_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ List all TaskRuns of Task 'foo' in namespace 'bar':
--label string A selector (label query) to filter on, supports '=', '==', and '!='
--limit int limit taskruns listed (default: return all taskruns)
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--reverse list taskruns in reverse order
--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-pipelinerun-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Lists pipelineruns in a namespace
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.

.PP
\fB\-\-reverse\fP[=false]
list pipelineruns in reverse order

.PP
\fB\-\-template\fP=""
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-taskrun-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Lists TaskRuns in a namespace
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.

.PP
\fB\-\-reverse\fP[=false]
list taskruns in reverse order

.PP
\fB\-\-template\fP=""
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
Expand Down
18 changes: 18 additions & 0 deletions pkg/cmd/pipelinerun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
type ListOptions struct {
Limit int
LabelSelector string
Reverse bool
}

func listCommand(p cli.Params) *cobra.Command {
Expand Down Expand Up @@ -84,6 +85,10 @@ List all PipelineRuns in a namespace 'foo':
return err
}

if prs != nil && opts.Reverse {
reverse(prs)
}

output, err := cmd.LocalFlags().GetString("output")
if err != nil {
fmt.Fprint(os.Stderr, "Error: output option not set properly \n")
Expand Down Expand Up @@ -122,6 +127,7 @@ List all PipelineRuns in a namespace 'foo':
f.AddFlags(c)
c.Flags().IntVarP(&opts.Limit, "limit", "", 0, "limit pipelineruns listed (default: return all pipelineruns)")
c.Flags().StringVarP(&opts.LabelSelector, "label", "", opts.LabelSelector, "A selector (label query) to filter on, supports '=', '==', and '!='")
c.Flags().BoolVarP(&opts.Reverse, "reverse", "", opts.Reverse, "list pipelineruns in reverse order")
return c
}

Expand Down Expand Up @@ -183,6 +189,18 @@ func list(p cli.Params, pipeline string, limit int, labelselector string) (*v1al
return prs, nil
}

func reverse(prs *v1alpha1.PipelineRunList) {
i := 0
j := len(prs.Items) - 1
prItems := prs.Items
for i < j {
prItems[i], prItems[j] = prItems[j], prItems[i]
i++
j--
}
prs.Items = prItems
}

func printFormatted(s *cli.Stream, prs *v1alpha1.PipelineRunList, c clockwork.Clock) error {
if len(prs.Items) == 0 {
fmt.Fprintln(s.Err, emptyMsg)
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/pipelinerun/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ func TestListPipelineRuns(t *testing.T) {
args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}", "--limit", fmt.Sprintf("%d", 2)},
wantError: false,
},
{
name: "print in reverse",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "--reverse", "-n", "namespace"},
wantError: false,
},
{
name: "print in reverse with output flag",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "--reverse", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"},
wantError: false,
},
}

for _, td := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NAME STARTED DURATION STATUS
pr2-1 3 hours ago --- Succeeded(Running)
pr2-2 2 hours ago 1 minute Failed
pr1-1 59 minutes ago 1 minute Succeeded
pr3-1 --- --- ---
pr0-1 --- --- ---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr2-1
pr2-2
pr1-1
pr3-1
pr0-1
19 changes: 18 additions & 1 deletion pkg/cmd/taskrun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
type ListOptions struct {
Limit int
LabelSelector string
Reverse bool
}

func listCommand(p cli.Params) *cobra.Command {
Expand Down Expand Up @@ -84,6 +85,10 @@ List all TaskRuns of Task 'foo' in namespace 'bar':
return err
}

if trs != nil && opts.Reverse {
reverse(trs)
}

output, err := cmd.LocalFlags().GetString("output")
if err != nil {
fmt.Fprint(os.Stderr, "Error: output option not set properly \n")
Expand Down Expand Up @@ -123,10 +128,22 @@ List all TaskRuns of Task 'foo' in namespace 'bar':
f.AddFlags(c)
c.Flags().IntVarP(&opts.Limit, "limit", "", 0, "limit taskruns listed (default: return all taskruns)")
c.Flags().StringVarP(&opts.LabelSelector, "label", "", opts.LabelSelector, "A selector (label query) to filter on, supports '=', '==', and '!='")

c.Flags().BoolVarP(&opts.Reverse, "reverse", "", opts.Reverse, "list taskruns in reverse order")
return c
}

func reverse(trs *v1alpha1.TaskRunList) {
i := 0
j := len(trs.Items) - 1
trItems := trs.Items
for i < j {
trItems[i], trItems[j] = trItems[j], trItems[i]
i++
j--
}
trs.Items = trItems
}

func list(p cli.Params, task string, limit int, labelselector string) (*v1alpha1.TaskRunList, error) {
var selector string
var options v1.ListOptions
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/taskrun/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ func TestListTaskRuns(t *testing.T) {
args: []string{"list", "-n", "foo", "--label", "honey=nutella", "tr3-1"},
wantError: true,
},
{
name: "print in reverse",
command: command(t, trs, now, ns),
args: []string{"list", "--reverse", "-n", "foo"},
wantError: false,
},
{
name: "print in reverse with output flag",
command: command(t, trs, now, ns),
args: []string{"list", "--reverse", "-n", "foo", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"},
wantError: false,
},
}

for _, td := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NAME STARTED DURATION STATUS
tr2-1 1 hour ago --- Running
tr1-1 1 hour ago 1 minute Succeeded
tr2-2 59 minutes ago 1 minute Failed
tr3-1 --- --- Failed
tr0-1 --- --- Succeeded
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tr2-1
tr1-1
tr2-2
tr3-1
tr0-1

0 comments on commit 3591a56

Please sign in to comment.