Skip to content

Commit

Permalink
feat: add pagination for get cmds (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neal authored Jan 23, 2020
1 parent cfdf297 commit bb7ee8b
Show file tree
Hide file tree
Showing 40 changed files with 218 additions and 96 deletions.
21 changes: 17 additions & 4 deletions cmd/build/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ var GetCmd = cli.Command{
},

// optional flags that can be supplied to a command
cli.IntFlag{
Name: "page,p",
Usage: "Print a specific page of builds",
Value: 1,
},
cli.IntFlag{
Name: "per-page,pp",
Usage: "Expand the number of items contained within page",
Value: 10,
},
cli.StringFlag{
Name: "output,o",
Usage: "Print the output in wide, yaml or json format",
Expand Down Expand Up @@ -78,7 +88,13 @@ func get(c *cli.Context) error {
// set token from global config
client.Authentication.SetTokenAuth(c.GlobalString("token"))

builds, _, err := client.Build.GetAll(org, repo)
// set the page options based on user input
opts := &vela.ListOptions{
Page: c.Int("page"),
PerPage: c.Int("per-page"),
}

builds, _, err := client.Build.GetAll(org, repo, opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +160,6 @@ func get(c *cli.Context) error {

// calcDuration gets build duration
func calcDuration(b *library.Build) string {

dur := (b.GetFinished() - b.GetStarted())

if dur < 60 {
Expand All @@ -157,7 +172,6 @@ func calcDuration(b *library.Build) string {
// modifybuild changes the event data within the struct
// to reflect to custom output when we are not outputing yaml or json
func modifyBuild(b *library.Build) {

switch d := strings.Split(b.GetRef(), "/")[1]; d {
case "tags":
*b.Ref = "tag"
Expand All @@ -172,7 +186,6 @@ func modifyBuild(b *library.Build) {

// helper function to reverse the build list output
func reverse(b []library.Build) []library.Build {

sort.SliceStable(b, func(i, j int) bool {
return b[i].GetNumber() < b[j].GetNumber()
})
Expand Down
14 changes: 12 additions & 2 deletions cmd/build/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func init() {
}

func TestBuild_Get_Success(t *testing.T) {

set := flag.NewFlagSet("test", 0)
_ = cli.NewContext(testBuildAppGet, set, nil)

Expand Down Expand Up @@ -76,6 +75,18 @@ func TestBuild_Get_Success(t *testing.T) {
"", "--addr", s.URL, "--token", "foobar",
"get", "build",
"--org", "github", "--repo", "octocat", "--o", "wide"}, want: nil},

// page default output
{data: []string{
"", "--addr", s.URL, "--token", "foobar",
"get", "build",
"--org", "github", "--repo", "octocat", "--p", "2"}, want: nil},

// per page default output
{data: []string{
"", "--addr", s.URL, "--token", "foobar",
"get", "build",
"--org", "github", "--repo", "octocat", "--pp", "20"}, want: nil},
}

// run test
Expand All @@ -89,7 +100,6 @@ func TestBuild_Get_Success(t *testing.T) {
}

func TestBuild_Get_Failure(t *testing.T) {

set := flag.NewFlagSet("test", 0)
_ = cli.NewContext(testBuildAppGet, set, nil)

Expand Down
1 change: 0 additions & 1 deletion cmd/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ EXAMPLES:

// helper function to execute vela restart build cli command
func restart(c *cli.Context) error {

if c.Int("build-number") == 0 {
return util.InvalidCommand("build-number")
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/build/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func init() {
}

func TestBuild_Restart_Success(t *testing.T) {

set := flag.NewFlagSet("test", 0)
_ = cli.NewContext(testBuildAppRestart, set, nil)

Expand Down Expand Up @@ -70,7 +69,6 @@ func TestBuild_Restart_Success(t *testing.T) {
}

func TestBuild_Restart_Failure(t *testing.T) {

set := flag.NewFlagSet("test", 0)
_ = cli.NewContext(testBuildAppRestart, set, nil)

Expand Down
1 change: 0 additions & 1 deletion cmd/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
// helper function to load global configuration if set
// via config or environment and validate the user input in the command
func validate(c *cli.Context) error {

// load configuration
if len(c.String("org")) == 0 {
err := c.Set("org", c.GlobalString("org"))
Expand Down
1 change: 0 additions & 1 deletion cmd/build/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ EXAMPLES:

// helper function to execute vela info build cli command
func view(c *cli.Context) error {

if c.Int("build-number") == 0 {
return util.InvalidCommand("build-number")
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/build/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func init() {
}

func TestBuild_View_Success(t *testing.T) {

set := flag.NewFlagSet("test", 0)
_ = cli.NewContext(testBuildAppView, set, nil)

Expand Down Expand Up @@ -76,7 +75,6 @@ func TestBuild_View_Success(t *testing.T) {
}

func TestBuild_View_Failure(t *testing.T) {

set := flag.NewFlagSet("test", 0)
_ = cli.NewContext(testBuildAppView, set, nil)

Expand Down
5 changes: 4 additions & 1 deletion cmd/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ EXAMPLES:

// helper function to execute a add repo cli command
func add(c *cli.Context) error {

// get org and repo information from cmd flags
org, repo := c.String("org"), c.String("repo")

Expand All @@ -98,6 +97,7 @@ func add(c *cli.Context) error {
if err != nil {
return err
}

client.Authentication.SetTokenAuth(c.GlobalString("token"))

// resource to create on server
Expand All @@ -115,12 +115,15 @@ func add(c *cli.Context) error {
if event == constants.EventPush {
request.AllowPush = vela.Bool(true)
}

if event == constants.EventPull {
request.AllowPull = vela.Bool(true)
}

if event == constants.EventTag {
request.AllowTag = vela.Bool(true)
}

if event == constants.EventDeploy {
request.AllowDeploy = vela.Bool(true)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/repo/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestRepo_Add_Success(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down Expand Up @@ -89,6 +90,7 @@ func TestRepo_Add_Failure(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down
18 changes: 17 additions & 1 deletion cmd/repo/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ var GetCmd = cli.Command{
Flags: []cli.Flag{

// optional flags that can be supplied to a command
cli.IntFlag{
Name: "page,p",
Usage: "Print a specific page of repos",
Value: 1,
},
cli.IntFlag{
Name: "per-page,pp",
Usage: "Expand the number of items contained within page",
Value: 10,
},
cli.StringFlag{
Name: "output,o",
Usage: "Print the output in wide, yaml or json format",
Expand Down Expand Up @@ -57,7 +67,13 @@ func get(c *cli.Context) error {
// set token from global config
client.Authentication.SetTokenAuth(c.GlobalString("token"))

repositories, _, err := client.Repo.GetAll()
// set the page options based on user input
opts := &vela.ListOptions{
Page: c.Int("page"),
PerPage: c.Int("per-page"),
}

repositories, _, err := client.Repo.GetAll(opts)
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/repo/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestRepo_Get_Success(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand All @@ -70,6 +71,16 @@ func TestRepo_Get_Success(t *testing.T) {
{data: []string{
"", "--addr", s.URL, "--token", "foobar",
"get", "repo", "--o", "wide"}, want: nil},

// page default output
{data: []string{
"", "--addr", s.URL, "--token", "foobar",
"get", "repo", "--p", "2"}, want: nil},

// per page output
{data: []string{
"", "--addr", s.URL, "--token", "foobar",
"get", "repo", "--pp", "20"}, want: nil},
}

// run test
Expand All @@ -89,6 +100,7 @@ func TestRepo_Get_Failure(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down
1 change: 0 additions & 1 deletion cmd/repo/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ EXAMPLES:

// helper function to execute a remove repo cli command
func remove(c *cli.Context) error {

// get org and repo information from cmd flags
org, repo := c.String("org"), c.String("repo")

Expand Down
2 changes: 2 additions & 0 deletions cmd/repo/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestRepo_Remove_Success(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down Expand Up @@ -74,6 +75,7 @@ func TestRepo_Remove_Failure(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down
1 change: 0 additions & 1 deletion cmd/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ EXAMPLES:

// helper function to execute a update repo cli command
func update(c *cli.Context) error {

// get org and repo information from cmd flags
org, repo := c.String("org"), c.String("repo")

Expand Down
2 changes: 2 additions & 0 deletions cmd/repo/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestRepo_Update_Success(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down Expand Up @@ -87,6 +88,7 @@ func TestRepo_Update_Failure(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down
15 changes: 12 additions & 3 deletions cmd/repo/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@
package repo

import (
"fmt"

"github.com/go-vela/cli/util"
"github.com/urfave/cli"
)

// helper function to load global configuration if set
// via config or environment and validate the user input in the command
func validate(c *cli.Context) error {

// load configuration
if len(c.String("org")) == 0 {
c.Set("org", c.GlobalString("org"))
err := c.Set("org", c.GlobalString("org"))
if err != nil {
return fmt.Errorf("unable to set context: %w", err)
}
}

if len(c.String("repo")) == 0 {
c.Set("repo", c.GlobalString("repo"))
err := c.Set("repo", c.GlobalString("repo"))
if err != nil {
return fmt.Errorf("unable to set context: %w", err)
}
}

// validate the user input in the command
if len(c.String("org")) == 0 {
return util.InvalidCommand("org")
}

if len(c.String("repo")) == 0 {
return util.InvalidCommand("repo")
}
Expand Down
1 change: 0 additions & 1 deletion cmd/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ EXAMPLES:

// helper function to execute logs cli command
func view(c *cli.Context) error {

// get org and repo information from cmd flags
org, repo := c.String("org"), c.String("repo")

Expand Down
2 changes: 2 additions & 0 deletions cmd/repo/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestRepo_View_Success(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down Expand Up @@ -81,6 +82,7 @@ func TestRepo_View_Failure(t *testing.T) {

// setup server
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())

// setup types
Expand Down
Loading

0 comments on commit bb7ee8b

Please sign in to comment.