Skip to content

Commit 652ec23

Browse files
Simplified
1 parent 90e78ad commit 652ec23

File tree

2 files changed

+64
-72
lines changed

2 files changed

+64
-72
lines changed

internal/cmd/stack/list.go

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/shurcooL/graphql"
1010
"github.com/spacelift-io/spacectl/client/structs"
11+
"github.com/spacelift-io/spacectl/internal"
1112
"github.com/spacelift-io/spacectl/internal/cmd"
1213
"github.com/urfave/cli/v2"
1314
)
@@ -19,16 +20,29 @@ func listStacks() cli.ActionFunc {
1920
return err
2021
}
2122

22-
p, err := newPaging(cliCtx)
23-
if err != nil {
24-
return err
23+
var limit *uint
24+
if cliCtx.IsSet(flagLimit.Name) {
25+
if cliCtx.Uint(flagLimit.Name) == 0 {
26+
return fmt.Errorf("limit must be greater than 0")
27+
}
28+
29+
limit = internal.ToPtr(cliCtx.Uint(flagLimit.Name))
30+
}
31+
32+
var search *string
33+
if cliCtx.IsSet(flagSearch.Name) {
34+
if cliCtx.String(flagSearch.Name) == "" {
35+
return fmt.Errorf("search must be non-empty")
36+
}
37+
38+
search = internal.ToPtr(cliCtx.String(flagSearch.Name))
2539
}
2640

2741
switch outputFormat {
2842
case cmd.OutputFormatTable:
29-
return listStacksTable(cliCtx, p)
43+
return listStacksTable(cliCtx, search, limit)
3044
case cmd.OutputFormatJSON:
31-
return listStacksJSON(cliCtx, p)
45+
return listStacksJSON(cliCtx, search, limit)
3246
}
3347

3448
return fmt.Errorf("unknown output format: %v", outputFormat)
@@ -37,9 +51,23 @@ func listStacks() cli.ActionFunc {
3751

3852
func listStacksJSON(
3953
ctx *cli.Context,
40-
p paging,
54+
search *string,
55+
limit *uint,
4156
) error {
42-
stacks, err := searchAllStacks(ctx.Context, p.toSearchInput())
57+
var first *graphql.Int
58+
if limit != nil {
59+
first = graphql.NewInt(graphql.Int(*limit)) //nolint: gosec
60+
}
61+
62+
var fullTextSearch *graphql.String
63+
if search != nil {
64+
fullTextSearch = graphql.NewString(graphql.String(*search))
65+
}
66+
67+
stacks, err := searchAllStacks(ctx.Context, structs.SearchInput{
68+
First: first,
69+
FullTextSearch: fullTextSearch,
70+
})
4371
if err != nil {
4472
return err
4573
}
@@ -49,12 +77,26 @@ func listStacksJSON(
4977

5078
func listStacksTable(
5179
ctx *cli.Context,
52-
p paging,
80+
search *string,
81+
limit *uint,
5382
) error {
54-
input := p.toSearchInput()
55-
input.OrderBy = &structs.QueryOrder{
56-
Field: "starred",
57-
Direction: "DESC",
83+
var first *graphql.Int
84+
if limit != nil {
85+
first = graphql.NewInt(graphql.Int(*limit)) //nolint: gosec
86+
}
87+
88+
var fullTextSearch *graphql.String
89+
if search != nil {
90+
fullTextSearch = graphql.NewString(graphql.String(*search))
91+
}
92+
93+
input := structs.SearchInput{
94+
First: first,
95+
FullTextSearch: fullTextSearch,
96+
OrderBy: &structs.QueryOrder{
97+
Field: "starred",
98+
Direction: "DESC",
99+
},
58100
}
59101

60102
stacks, err := searchAllStacks(ctx.Context, input)
@@ -91,7 +133,9 @@ func listStacksTable(
91133
// searchStacks returns a list of stacks based on the provided search input.
92134
// input.First limits the total number of returned stacks, if not provided all stacks are returned.
93135
func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, error) {
94-
// 0 means no limit
136+
const firstMaxValue = 50
137+
138+
// 0 return all
95139
var total int
96140
if input.First != nil {
97141
total = int(*input.First)
@@ -100,13 +144,18 @@ func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, e
100144
out := []stack{}
101145

102146
pageInput := structs.SearchInput{
103-
First: graphql.NewInt(50),
147+
First: graphql.NewInt(firstMaxValue),
104148
FullTextSearch: input.FullTextSearch,
105149
}
106150
for {
107151
if total > 0 {
108152
// Fetch exactly the number of items requested
109-
pageInput.First = graphql.NewInt(graphql.Int(slices.Min([]int{50, total - len(out)})))
153+
pageInput.First = graphql.NewInt(
154+
//nolint: gosec
155+
graphql.Int(
156+
slices.Min([]int{firstMaxValue, total - len(out)}),
157+
),
158+
)
110159
}
111160

112161
result, err := searchStacks(ctx, pageInput)

internal/cmd/stack/paging.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)