8
8
9
9
"github.com/shurcooL/graphql"
10
10
"github.com/spacelift-io/spacectl/client/structs"
11
+ "github.com/spacelift-io/spacectl/internal"
11
12
"github.com/spacelift-io/spacectl/internal/cmd"
12
13
"github.com/urfave/cli/v2"
13
14
)
@@ -19,16 +20,29 @@ func listStacks() cli.ActionFunc {
19
20
return err
20
21
}
21
22
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 ))
25
39
}
26
40
27
41
switch outputFormat {
28
42
case cmd .OutputFormatTable :
29
- return listStacksTable (cliCtx , p )
43
+ return listStacksTable (cliCtx , search , limit )
30
44
case cmd .OutputFormatJSON :
31
- return listStacksJSON (cliCtx , p )
45
+ return listStacksJSON (cliCtx , search , limit )
32
46
}
33
47
34
48
return fmt .Errorf ("unknown output format: %v" , outputFormat )
@@ -37,9 +51,23 @@ func listStacks() cli.ActionFunc {
37
51
38
52
func listStacksJSON (
39
53
ctx * cli.Context ,
40
- p paging ,
54
+ search * string ,
55
+ limit * uint ,
41
56
) 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
+ })
43
71
if err != nil {
44
72
return err
45
73
}
@@ -49,12 +77,26 @@ func listStacksJSON(
49
77
50
78
func listStacksTable (
51
79
ctx * cli.Context ,
52
- p paging ,
80
+ search * string ,
81
+ limit * uint ,
53
82
) 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
+ },
58
100
}
59
101
60
102
stacks , err := searchAllStacks (ctx .Context , input )
@@ -91,7 +133,9 @@ func listStacksTable(
91
133
// searchStacks returns a list of stacks based on the provided search input.
92
134
// input.First limits the total number of returned stacks, if not provided all stacks are returned.
93
135
func searchAllStacks (ctx context.Context , input structs.SearchInput ) ([]stack , error ) {
94
- // 0 means no limit
136
+ const firstMaxValue = 50
137
+
138
+ // 0 return all
95
139
var total int
96
140
if input .First != nil {
97
141
total = int (* input .First )
@@ -100,13 +144,18 @@ func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, e
100
144
out := []stack {}
101
145
102
146
pageInput := structs.SearchInput {
103
- First : graphql .NewInt (50 ),
147
+ First : graphql .NewInt (firstMaxValue ),
104
148
FullTextSearch : input .FullTextSearch ,
105
149
}
106
150
for {
107
151
if total > 0 {
108
152
// 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
+ )
110
159
}
111
160
112
161
result , err := searchStacks (ctx , pageInput )
0 commit comments