Skip to content

Commit

Permalink
Change flag behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Santos committed Feb 8, 2024
1 parent cefddda commit 5f9b1af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs/stackit_dns_zone_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ stackit dns zone list [flags]

```
--active Filter for active zones
--deleted Filter for deleted zones
-h, --help Help for "stackit dns zone list"
--inactive Filter for inactive zones. Deleted zones are always inactive and will be included when this flag is set
--inactive Filter for inactive zones
--include-deleted Includes successfully deleted zones (if unset, these are filtered out)
--limit int Maximum number of entries to list
--name-like string Filter by name
--order-by-name string Order by name, one of ["asc" "desc"]
Expand Down
42 changes: 20 additions & 22 deletions internal/cmd/dns/zone/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
)

const (
activeFlag = "active"
inactiveFlag = "inactive"
deletedFlag = "deleted"
nameLikeFlag = "name-like"
orderByNameFlag = "order-by-name"
limitFlag = "limit"
pageSizeFlag = "page-size"
activeFlag = "active"
inactiveFlag = "inactive"
nameLikeFlag = "name-like"
orderByNameFlag = "order-by-name"
includeDeletedFlag = "include-deleted"
limitFlag = "limit"
pageSizeFlag = "page-size"

pageSizeDefault = 100
deleteSucceededState = "DELETE_SUCCEEDED"
Expand All @@ -35,13 +35,13 @@ const (
type inputModel struct {
*globalflags.GlobalFlagModel

Active bool
Inactive bool
Deleted bool
NameLike *string
OrderByName *string
Limit *int64
PageSize int64
Active bool
Inactive bool
NameLike *string
OrderByName *string
IncludeDeleted bool
Limit *int64
PageSize int64
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -102,10 +102,10 @@ func configureFlags(cmd *cobra.Command) {
orderByNameFlagOptions := []string{"asc", "desc"}

cmd.Flags().Bool(activeFlag, false, "Filter for active zones")
cmd.Flags().Bool(inactiveFlag, false, "Filter for inactive zones. Deleted zones are always inactive and will be included when this flag is set")
cmd.Flags().Bool(deletedFlag, false, "Filter for deleted zones")
cmd.Flags().Bool(inactiveFlag, false, "Filter for inactive zones")
cmd.Flags().String(nameLikeFlag, "", "Filter by name")
cmd.Flags().Var(flags.EnumFlag(true, "", orderByNameFlagOptions...), orderByNameFlag, fmt.Sprintf("Order by name, one of %q", orderByNameFlagOptions))
cmd.Flags().Bool(includeDeletedFlag, false, "Includes successfully deleted zones (if unset, these are filtered out)")
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
cmd.Flags().Int64(pageSizeFlag, pageSizeDefault, "Number of items fetched in each API call. Does not affect the number of items in the command output")
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) {
GlobalFlagModel: globalFlags,
Active: active,
Inactive: inactive,
Deleted: flags.FlagToBoolValue(cmd, deletedFlag),
IncludeDeleted: flags.FlagToBoolValue(cmd, includeDeletedFlag),
NameLike: flags.FlagToStringPointer(cmd, nameLikeFlag),
OrderByName: flags.FlagToStringPointer(cmd, orderByNameFlag),
Limit: limit,
Expand All @@ -158,17 +158,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient dnsClient, p
if model.Inactive {
req = req.ActiveEq(false)
}
if model.Deleted {
req = req.StateEq(deleteSucceededState)
} else if !model.Inactive {
req = req.StateNeq(deleteSucceededState)
}
if model.NameLike != nil {
req = req.NameLike(*model.NameLike)
}
if model.OrderByName != nil {
req = req.OrderByName(strings.ToUpper(*model.OrderByName))
}
if !model.IncludeDeleted {
req = req.StateNeq(deleteSucceededState)
}
req = req.PageSize(int32(model.PageSize))
req = req.Page(int32(page))
return req
Expand Down
14 changes: 7 additions & 7 deletions internal/cmd/dns/zone/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func TestParseInput(t *testing.T) {
expectedModel: fixtureInputModel(),
},
{
description: "deleted zones",
description: "include deleted zones",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[deletedFlag] = "true"
flagValues[includeDeletedFlag] = "true"
}),
isValid: true,
expectedModel: fixtureInputModel(func(model *inputModel) {
model.Deleted = true
model.IncludeDeleted = true
}),
},
{
Expand Down Expand Up @@ -269,12 +269,12 @@ func TestBuildRequest(t *testing.T) {
expectedRequest: fixtureRequest().StateNeq(deleteSucceededState).Page(10),
},
{
description: "deleted zones",
description: "include deleted zones",
model: fixtureInputModel(func(model *inputModel) {
model.Deleted = true
model.IncludeDeleted = true
}),
page: 1,
expectedRequest: fixtureRequest().StateEq(deleteSucceededState).Page(1),
expectedRequest: fixtureRequest().Page(1),
},
{
description: "active zones",
Expand All @@ -290,7 +290,7 @@ func TestBuildRequest(t *testing.T) {
model.Inactive = true
}),
page: 1,
expectedRequest: fixtureRequest().ActiveEq(false).Page(1),
expectedRequest: fixtureRequest().ActiveEq(false).StateNeq(deleteSucceededState).Page(1),
},
{
description: "required fields only",
Expand Down

0 comments on commit 5f9b1af

Please sign in to comment.