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

feat: add paging flags for list commands that support paging #297

Merged
merged 4 commits into from
Apr 4, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add managed object storage name to `show` and `list` command outputs.
- Add completions for managed object storages and allow using managed object storage name (in addition to its UUID) as a positional argument.
- Add paging flags (`--limit` and `--page`) to `database`, `gateway`, `loadbalancer`, and `objectstorage` list commands

## [3.6.0] - 2024-03-07

Expand Down
13 changes: 12 additions & 1 deletion internal/commands/database/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/paging"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)

// ListCommand creates the "database list" command
Expand All @@ -17,12 +19,21 @@ func ListCommand() commands.Command {

type listCommand struct {
*commands.BaseCommand
paging.PageParameters
}

func (s *listCommand) InitCommand() {
fs := &pflag.FlagSet{}
s.ConfigureFlags(fs)
s.AddFlags(fs)
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
databases, err := svc.GetManagedDatabases(exec.Context(), &request.GetManagedDatabasesRequest{})
databases, err := svc.GetManagedDatabases(exec.Context(), &request.GetManagedDatabasesRequest{
Page: s.Page(),
})
if err != nil {
return nil, err
}
Expand Down
53 changes: 37 additions & 16 deletions internal/commands/database/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,50 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestDatabaseListTitleFallback(t *testing.T) {
text.DisableColors()
databases := []upcloud.ManagedDatabase{
{UUID: "091f1afe-4ddd-4d43-afad-6aa3069cc7fe", Title: "service-name", Name: "hotname-prefix-1", State: "running"},
{UUID: "091f1afe-4ddd-4d43-afad-6aa3069cc7fe", Name: "hotname-prefix-2", State: "running"},
{UUID: "091f1afe-4ddd-4d43-afad-6aa3069cc7fe", Title: "service-name", Name: "hostname-prefix-1", State: "running"},
{UUID: "091f1afe-4ddd-4d43-afad-6aa3069cc7fe", Name: "hostname-prefix-2", State: "running"},
}

mService := smock.Service{}
mService.On("GetManagedDatabases", mock.Anything).Return(databases, nil)

conf := config.New()
command := commands.BuildCommand(ListCommand(), nil, conf)

output, err := mockexecute.MockExecute(command, &mService, conf)

assert.NoError(t, err)
assert.Regexp(t, `UUID\s+Title\s+Type\s+Plan\s+Zone\s+State`, output)
assert.Contains(t, output, "service-name")
assert.NotContains(t, output, "hotname-prefix-1")
assert.Contains(t, output, "hotname-prefix-2")
for _, test := range []struct {
name string
args []string
page request.Page
}{
{
name: "default page",
page: request.Page{Size: 100, Number: 0},
},
{
name: "limit and page args",
page: request.Page{Size: 18, Number: 19},
args: []string{"--limit", "18", "--page", "19"},
},
} {
t.Run(test.name, func(t *testing.T) {
page := test.page

mService := smock.Service{}
mService.On("GetManagedDatabases", &request.GetManagedDatabasesRequest{Page: &page}).Return(databases, nil)

conf := config.New()
command := commands.BuildCommand(ListCommand(), nil, conf)
command.Cobra().SetArgs(test.args)

output, err := mockexecute.MockExecute(command, &mService, conf)

assert.NoError(t, err)
assert.Regexp(t, `UUID\s+Title\s+Type\s+Plan\s+Zone\s+State`, output)
assert.Contains(t, output, "service-name")
assert.NotContains(t, output, "hostname-prefix-1")
assert.Contains(t, output, "hostname-prefix-2")
})
}
}
11 changes: 10 additions & 1 deletion internal/commands/gateway/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/paging"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/pflag"
)

// ListCommand creates the "gateway list" command
Expand All @@ -20,12 +22,19 @@ func ListCommand() commands.Command {

type listCommand struct {
*commands.BaseCommand
paging.PageParameters
}

func (c *listCommand) InitCommand() {
fs := &pflag.FlagSet{}
c.PageParameters.ConfigureFlags(fs)
c.AddFlags(fs)
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
gateways, err := svc.GetGateways(exec.Context())
gateways, err := svc.GetGateways(exec.Context(), c.Page())
if err != nil {
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion internal/commands/loadbalancer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/paging"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)

// ListCommand creates the "loadbalancer list" command
Expand All @@ -17,12 +19,21 @@ func ListCommand() commands.Command {

type listCommand struct {
*commands.BaseCommand
paging.PageParameters
}

func (s *listCommand) InitCommand() {
fs := &pflag.FlagSet{}
s.ConfigureFlags(fs)
s.AddFlags(fs)
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
loadbalancers, err := svc.GetLoadBalancers(exec.Context(), &request.GetLoadBalancersRequest{})
loadbalancers, err := svc.GetLoadBalancers(exec.Context(), &request.GetLoadBalancersRequest{
Page: s.Page(),
})
if err != nil {
return nil, err
}
Expand Down
13 changes: 12 additions & 1 deletion internal/commands/objectstorage/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/paging"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/spf13/pflag"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
Expand All @@ -18,12 +20,21 @@ func ListCommand() commands.Command {

type listCommand struct {
*commands.BaseCommand
paging.PageParameters
}

func (c *listCommand) InitCommand() {
fs := &pflag.FlagSet{}
c.ConfigureFlags(fs)
c.AddFlags(fs)
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (c *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
svc := exec.All()
objectstorages, err := svc.GetManagedObjectStorages(exec.Context(), &request.GetManagedObjectStoragesRequest{})
objectstorages, err := svc.GetManagedObjectStorages(exec.Context(), &request.GetManagedObjectStoragesRequest{
Page: c.Page(),
})
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions internal/paging/paging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package paging
kangasta marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)

type PageParameters struct {
size int
number int
}

func (pp *PageParameters) ConfigureFlags(fs *pflag.FlagSet) {
fs.IntVar(&pp.size, "limit", 100, "Number of entries to receive at most.")
fs.IntVar(&pp.number, "page", 0, "Page number to calculate first item to receive. Page numbers start from `1`.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could also be --page-size and --page-number to be more descriptive, but those are quite verbose 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As they naturally are available only on list commands, these shorter ones seem fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also thinking between --size and --limit. Go SDK uses size, but there that is used as field of Page struct. Maybe limit would clearer without that context 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed --size--limit

}

func (pp *PageParameters) Page() *request.Page {
return &request.Page{
Number: pp.number,
Size: pp.size,
}
}
Loading