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

Implement --limit flag on list commands #18

Merged
merged 2 commits into from
Nov 23, 2023
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
15 changes: 15 additions & 0 deletions internal/cmd/dns/record-set/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
nameLikeFlag = "name-like"
activeFlag = "is-active"
orderByNameFlag = "order-by-name"
limitFlag = "limit"
)

type flagModel struct {
Expand All @@ -30,6 +31,7 @@ type flagModel struct {
NameLike *string
Active *bool
OrderByName *string
Limit *int64
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -63,6 +65,11 @@ func NewCmd() *cobra.Command {
return nil
}

// Truncate output
if model.Limit != nil && len(recordSets) > int(*model.Limit) {
recordSets = recordSets[:*model.Limit]
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID", "Name", "Type", "State")
Expand All @@ -75,6 +82,7 @@ func NewCmd() *cobra.Command {
return nil
},
}

configureFlags(cmd)
return cmd
}
Expand All @@ -87,6 +95,7 @@ func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(nameLikeFlag, "", "Filter by name")
cmd.Flags().Var(flags.EnumBoolFlag(), activeFlag, fmt.Sprintf("Filter by active status, one of %q", activeFlagOptions))
cmd.Flags().Var(flags.EnumFlag(true, orderByNameFlagOptions...), orderByNameFlag, fmt.Sprintf("Order by name, one of %q", orderByNameFlagOptions))
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")

err := utils.MarkFlagsRequired(cmd, zoneIdFlag)
cobra.CheckErr(err)
Expand All @@ -98,12 +107,18 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
return nil, fmt.Errorf("project ID not set")
}

limit := utils.FlagToInt64Pointer(cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, fmt.Errorf("limit must be greater than 0")
}

return &flagModel{
ProjectId: projectId,
ZoneId: utils.FlagToStringValue(cmd, zoneIdFlag),
NameLike: utils.FlagToStringPointer(cmd, nameLikeFlag),
Active: utils.FlagToBoolPointer(cmd, activeFlag),
OrderByName: utils.FlagToStringPointer(cmd, orderByNameFlag),
Limit: utils.FlagToInt64Pointer(cmd, limitFlag),
}, nil
}

Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/dns/record-set/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
nameLikeFlag: "some-pattern",
activeFlag: "true",
orderByNameFlag: "asc",
limitFlag: "10",
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -43,6 +44,7 @@ func fixtureFlagModel(mods ...func(model *flagModel)) *flagModel {
NameLike: utils.Ptr("some-pattern"),
Active: utils.Ptr(true),
OrderByName: utils.Ptr("asc"),
Limit: utils.Ptr(int64(10)),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -170,6 +172,20 @@ func TestParseFlags(t *testing.T) {
}),
isValid: false,
},
{
description: "limit invalid",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "invalid"
}),
isValid: false,
},
{
description: "limit invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "0"
}),
isValid: false,
},
}

for _, tt := range tests {
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/dns/zone/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const (
nameLikeFlag = "name-like"
activeFlag = "is-active"
orderByNameFlag = "order-by-name"
limitFlag = "limit"
)

type flagModel struct {
ProjectId string
NameLike *string
Active *bool
OrderByName *string
Limit *int64
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -61,6 +63,11 @@ func NewCmd() *cobra.Command {
return nil
}

// Truncate output
if model.Limit != nil && len(zones) > int(*model.Limit) {
zones = zones[:*model.Limit]
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID", "NAME", "DNS_NAME", "STATE")
Expand All @@ -84,6 +91,7 @@ func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(nameLikeFlag, "", "Filter by name")
cmd.Flags().Var(flags.EnumBoolFlag(), activeFlag, fmt.Sprintf("Filter by active status, one of %q", activeFlagOptions))
cmd.Flags().Var(flags.EnumFlag(true, orderByNameFlagOptions...), orderByNameFlag, fmt.Sprintf("Order by name, one of %q", orderByNameFlagOptions))
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
Expand All @@ -92,11 +100,17 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
return nil, fmt.Errorf("project ID not set")
}

limit := utils.FlagToInt64Pointer(cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, fmt.Errorf("limit must be greater than 0")
}

return &flagModel{
ProjectId: projectId,
NameLike: utils.FlagToStringPointer(cmd, nameLikeFlag),
Active: utils.FlagToBoolPointer(cmd, activeFlag),
OrderByName: utils.FlagToStringPointer(cmd, orderByNameFlag),
Limit: limit,
}, nil
}

Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/dns/zone/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
nameLikeFlag: "some-pattern",
activeFlag: "true",
orderByNameFlag: "asc",
limitFlag: "10",
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -40,6 +41,7 @@ func fixtureFlagModel(mods ...func(model *flagModel)) *flagModel {
NameLike: utils.Ptr("some-pattern"),
Active: utils.Ptr(true),
OrderByName: utils.Ptr("asc"),
Limit: utils.Ptr(int64(10)),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -165,6 +167,20 @@ func TestParseFlags(t *testing.T) {
}),
isValid: false,
},
{
description: "limit invalid",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "invalid"
}),
isValid: false,
},
{
description: "limit invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "0"
}),
isValid: false,
},
}

for _, tt := range tests {
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/postgresql/credential/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import (
const (
projectIdFlag = "project-id"
instanceIdFlag = "instance-id"
limitFlag = "limit"
)

type flagModel struct {
ProjectId string
InstanceId string
Limit *int64
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -56,6 +58,11 @@ func NewCmd() *cobra.Command {
return nil
}

// Truncate output
if model.Limit != nil && len(credentials) > int(*model.Limit) {
credentials = credentials[:*model.Limit]
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID")
Expand All @@ -74,6 +81,7 @@ func NewCmd() *cobra.Command {

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")

err := utils.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
Expand All @@ -85,9 +93,15 @@ func parseFlags(cmd *cobra.Command) (*flagModel, error) {
return nil, fmt.Errorf("project ID not set")
}

limit := utils.FlagToInt64Pointer(cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, fmt.Errorf("limit must be greater than 0")
}

return &flagModel{
ProjectId: projectId,
InstanceId: utils.FlagToStringValue(cmd, instanceIdFlag),
Limit: limit,
}, nil
}

Expand Down
17 changes: 17 additions & 0 deletions internal/cmd/postgresql/credential/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand All @@ -25,6 +26,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
flagValues := map[string]string{
projectIdFlag: testProjectId,
instanceIdFlag: testInstanceId,
limitFlag: "10",
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -36,6 +38,7 @@ func fixtureFlagModel(mods ...func(model *flagModel)) *flagModel {
model := &flagModel{
ProjectId: testProjectId,
InstanceId: testInstanceId,
Limit: utils.Ptr(int64(10)),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -111,6 +114,20 @@ func TestParseFlags(t *testing.T) {
}),
isValid: false,
},
{
description: "limit invalid",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "invalid"
}),
isValid: false,
},
{
description: "limit invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "0"
}),
isValid: false,
},
}

for _, tt := range tests {
Expand Down
25 changes: 24 additions & 1 deletion internal/cmd/postgresql/instance/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresql/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/postgresql"
)

const (
limitFlag = "limit"
)

type flagModel struct {
ProjectId string
Limit *int64
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -48,6 +54,11 @@ func NewCmd() *cobra.Command {
return nil
}

// Truncate output
if model.Limit != nil && len(instances) > int(*model.Limit) {
instances = instances[:*model.Limit]
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID", "NAME", "LAST_OPERATION.TYPE", "LAST_OPERATION.STATE")
Expand All @@ -60,17 +71,29 @@ func NewCmd() *cobra.Command {
return nil
},
}

configureFlags(cmd)
return cmd
}

func parseFlags(_ *cobra.Command) (*flagModel, error) {
func configureFlags(cmd *cobra.Command) {
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}

limit := utils.FlagToInt64Pointer(cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, fmt.Errorf("limit must be greater than 0")
}

return &flagModel{
ProjectId: projectId,
Limit: utils.FlagToInt64Pointer(cmd, limitFlag),
}, nil
}

Expand Down
Loading