Skip to content

Commit

Permalink
Simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 committed Aug 29, 2024
1 parent 90e78ad commit 652ec23
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 72 deletions.
79 changes: 64 additions & 15 deletions internal/cmd/stack/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/shurcooL/graphql"
"github.com/spacelift-io/spacectl/client/structs"
"github.com/spacelift-io/spacectl/internal"
"github.com/spacelift-io/spacectl/internal/cmd"
"github.com/urfave/cli/v2"
)
Expand All @@ -19,16 +20,29 @@ func listStacks() cli.ActionFunc {
return err
}

p, err := newPaging(cliCtx)
if err != nil {
return err
var limit *uint
if cliCtx.IsSet(flagLimit.Name) {
if cliCtx.Uint(flagLimit.Name) == 0 {
return fmt.Errorf("limit must be greater than 0")
}

limit = internal.ToPtr(cliCtx.Uint(flagLimit.Name))
}

var search *string
if cliCtx.IsSet(flagSearch.Name) {
if cliCtx.String(flagSearch.Name) == "" {
return fmt.Errorf("search must be non-empty")
}

search = internal.ToPtr(cliCtx.String(flagSearch.Name))
}

switch outputFormat {
case cmd.OutputFormatTable:
return listStacksTable(cliCtx, p)
return listStacksTable(cliCtx, search, limit)
case cmd.OutputFormatJSON:
return listStacksJSON(cliCtx, p)
return listStacksJSON(cliCtx, search, limit)
}

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

func listStacksJSON(
ctx *cli.Context,
p paging,
search *string,
limit *uint,
) error {
stacks, err := searchAllStacks(ctx.Context, p.toSearchInput())
var first *graphql.Int
if limit != nil {
first = graphql.NewInt(graphql.Int(*limit)) //nolint: gosec
}

var fullTextSearch *graphql.String
if search != nil {
fullTextSearch = graphql.NewString(graphql.String(*search))
}

stacks, err := searchAllStacks(ctx.Context, structs.SearchInput{
First: first,
FullTextSearch: fullTextSearch,
})
if err != nil {
return err
}
Expand All @@ -49,12 +77,26 @@ func listStacksJSON(

func listStacksTable(
ctx *cli.Context,
p paging,
search *string,
limit *uint,
) error {
input := p.toSearchInput()
input.OrderBy = &structs.QueryOrder{
Field: "starred",
Direction: "DESC",
var first *graphql.Int
if limit != nil {
first = graphql.NewInt(graphql.Int(*limit)) //nolint: gosec
}

var fullTextSearch *graphql.String
if search != nil {
fullTextSearch = graphql.NewString(graphql.String(*search))
}

input := structs.SearchInput{
First: first,
FullTextSearch: fullTextSearch,
OrderBy: &structs.QueryOrder{
Field: "starred",
Direction: "DESC",
},
}

stacks, err := searchAllStacks(ctx.Context, input)
Expand Down Expand Up @@ -91,7 +133,9 @@ func listStacksTable(
// searchStacks returns a list of stacks based on the provided search input.
// input.First limits the total number of returned stacks, if not provided all stacks are returned.
func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, error) {
// 0 means no limit
const firstMaxValue = 50

// 0 return all
var total int
if input.First != nil {
total = int(*input.First)
Expand All @@ -100,13 +144,18 @@ func searchAllStacks(ctx context.Context, input structs.SearchInput) ([]stack, e
out := []stack{}

pageInput := structs.SearchInput{
First: graphql.NewInt(50),
First: graphql.NewInt(firstMaxValue),
FullTextSearch: input.FullTextSearch,
}
for {
if total > 0 {
// Fetch exactly the number of items requested
pageInput.First = graphql.NewInt(graphql.Int(slices.Min([]int{50, total - len(out)})))
pageInput.First = graphql.NewInt(
//nolint: gosec
graphql.Int(
slices.Min([]int{firstMaxValue, total - len(out)}),
),
)
}

result, err := searchStacks(ctx, pageInput)
Expand Down
57 changes: 0 additions & 57 deletions internal/cmd/stack/paging.go

This file was deleted.

0 comments on commit 652ec23

Please sign in to comment.