woodpecker/cli/build/build_list.go
6543 798c2bc8b2
Upgrade urfave/cli to v2 (#483)
* migrate urfave/ci v1 -> v2
* refactor cli (format flag)
* log error if agent can not listen on port 3000

close #452
2021-10-27 21:03:14 +02:00

96 lines
1.8 KiB
Go

package build
import (
"os"
"text/template"
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var buildListCmd = &cli.Command{
Name: "ls",
Usage: "show build history",
ArgsUsage: "<repo/name>",
Action: buildList,
Flags: append(common.GlobalFlags,
common.FormatFlag(tmplBuildList),
&cli.StringFlag{
Name: "branch",
Usage: "branch filter",
},
&cli.StringFlag{
Name: "event",
Usage: "event filter",
},
&cli.StringFlag{
Name: "status",
Usage: "status filter",
},
&cli.IntFlag{
Name: "limit",
Usage: "limit the list size",
Value: 25,
},
),
}
func buildList(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
builds, err := client.BuildList(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil {
return err
}
branch := c.String("branch")
event := c.String("event")
status := c.String("status")
limit := c.Int("limit")
var count int
for _, build := range builds {
if count >= limit {
break
}
if branch != "" && build.Branch != branch {
continue
}
if event != "" && build.Event != event {
continue
}
if status != "" && build.Status != status {
continue
}
tmpl.Execute(os.Stdout, build)
count++
}
return nil
}
// template for build list information
var tmplBuildList = "\x1b[33mBuild #{{ .Number }} \x1b[0m" + `
Status: {{ .Status }}
Event: {{ .Event }}
Commit: {{ .Commit }}
Branch: {{ .Branch }}
Ref: {{ .Ref }}
Author: {{ .Author }} {{ if .Email }}<{{.Email}}>{{ end }}
Message: {{ .Message }}
`