Skip to content

Commit

Permalink
sort pipelineruns for logs by start time and add limit flag
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and tekton-robot committed Aug 23, 2019
1 parent ae51d5f commit 6de3a8f
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 16 deletions.
9 changes: 5 additions & 4 deletions docs/cmd/tkn_pipeline_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ Show pipeline logs
### Options

```
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
-l, --last show logs for last run
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
-l, --last show logs for last run
-L, --limit int lists number of pipelineruns (default 5)
```

### Options inherited from parent commands
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-pipeline-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Show pipeline logs
\fB\-l\fP, \fB\-\-last\fP[=false]
show logs for last run

.PP
\fB\-L\fP, \fB\-\-limit\fP=5
lists number of pipelineruns


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
Expand Down
55 changes: 52 additions & 3 deletions pkg/cmd/pipeline/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pipeline
import (
"fmt"
"os"
"sort"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -38,6 +39,7 @@ type logOptions struct {
follow bool
pipelineName string
runName string
limit int
}

func nameArg(args []string, p cli.Params) error {
Expand Down Expand Up @@ -103,6 +105,7 @@ func logCommand(p cli.Params) *cobra.Command {
c.Flags().BoolVarP(&opts.last, "last", "l", false, "show logs for last run")
c.Flags().BoolVarP(&opts.allSteps, "all", "a", false, "show all logs including init steps injected by tekton")
c.Flags().BoolVarP(&opts.follow, "follow", "f", false, "stream live logs")
c.Flags().IntVarP(&opts.limit, "limit", "L", 5, "lists number of pipelineruns")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_pipeline")
return c
Expand Down Expand Up @@ -157,6 +160,12 @@ func (opts *logOptions) init(args []string) error {
}

func (opts *logOptions) getAllInputs() error {
err := validate(opts)

if err != nil {
return err
}

ps, err := allPipelines(opts)
if err != nil {
return err
Expand Down Expand Up @@ -185,9 +194,14 @@ func (opts *logOptions) getAllInputs() error {
}

func (opts *logOptions) askRunName() error {
err := validate(opts)
if err != nil {
return err
}

var ans string

prs, err := allRuns(opts.params, opts.pipelineName)
prs, err := allRuns(opts.params, opts.pipelineName, opts.limit)
if err != nil {
return err
}
Expand Down Expand Up @@ -246,7 +260,7 @@ func allPipelines(opts *logOptions) ([]string, error) {
return ret, nil
}

func allRuns(p cli.Params, pName string) ([]string, error) {
func allRuns(p cli.Params, pName string, limit int) ([]string, error) {
cs, err := p.Clients()
if err != nil {
return nil, err
Expand All @@ -260,9 +274,19 @@ func allRuns(p cli.Params, pName string) ([]string, error) {
return nil, err
}

runslen := len(runs.Items)

if runslen > 1 {
sort.Sort(byStartTime(runs.Items))
}

if limit > runslen {
limit = runslen
}

ret := []string{}
for i, run := range runs.Items {
if i < 5 {
if i < limit {
ret = append(ret, run.ObjectMeta.Name+" started "+formatted.Age(run.Status.StartTime, p.Time()))
}
}
Expand Down Expand Up @@ -291,3 +315,28 @@ func lastRun(cs *cli.Clients, ns, pName string) (string, error) {
}
return latest.ObjectMeta.Name, nil
}

type byStartTime []v1alpha1.PipelineRun

func (s byStartTime) Len() int { return len(s) }
func (s byStartTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byStartTime) Less(i, j int) bool {
if s[j].Status.StartTime == nil {
return false
}

if s[i].Status.StartTime == nil {
return true
}

return s[j].Status.StartTime.Before(s[i].Status.StartTime)
}

func validate(opts *logOptions) error {

if opts.limit <= 0 {
return fmt.Errorf("limit was %d but must be a positive number", opts.limit)
}

return nil
}
Loading

0 comments on commit 6de3a8f

Please sign in to comment.