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

Add search for modules #257

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
109 changes: 81 additions & 28 deletions internal/cmd/module/list.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package module

import (
"context"
"fmt"
"sort"
"strings"
"slices"

"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"github.com/spacelift-io/spacectl/client/structs"
"github.com/spacelift-io/spacectl/internal/cmd"
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
"github.com/urfave/cli/v2"
)

const (
maxSearchModulesPageSize = 50
)

func listModules() cli.ActionFunc {
return func(cliCtx *cli.Context) error {
outputFormat, err := cmd.GetOutputFormat(cliCtx)
Expand All @@ -21,52 +25,92 @@ func listModules() cli.ActionFunc {

switch outputFormat {
case cmd.OutputFormatTable:
return listModulesTable(cliCtx)
m, err := getModules(cliCtx, 20)
if err != nil {
return err
}
return listModulesTable(m)
case cmd.OutputFormatJSON:
return listModulesJSON(cliCtx.Context)
m, err := getModules(cliCtx, 0)
if err != nil {
return err
}
return cmd.OutputJSON(m)
}

return fmt.Errorf("unknown output format: %v", outputFormat)
}
}

func listModulesJSON(ctx context.Context) error {
var query struct {
Modules []module `graphql:"modules" json:"modules,omitempty"`
func getModules(cliCtx *cli.Context, limit int) ([]module, error) {
if limit < 0 {
return nil, errors.New("limit must be greater or equal to 0")
}

if err := authenticated.Client.Query(ctx, &query, map[string]interface{}{}); err != nil {
return errors.Wrap(err, "failed to query list of modules")
var cursor string
var modules []module

for {
pageSize := 50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pageSize := 50
pageSize := maxSearchModulesPageSize

if limit != 0 {
pageSize = slices.Min([]int{maxSearchModulesPageSize, limit - len(modules)})
}

result, err := getSearchModules(cliCtx, cursor, pageSize)
if err != nil {
return nil, err
}

for _, edge := range result.Edges {
modules = append(modules, edge.Node)
}

if result.PageInfo.HasNextPage && (limit == 0 || limit > len(modules)) {
cursor = result.PageInfo.EndCursor
continue
}

break
}
return cmd.OutputJSON(query.Modules)

return modules, nil
}

func listModulesTable(ctx *cli.Context) error {
func getSearchModules(cliCtx *cli.Context, cursor string, limit int) (searchModules, error) {
if limit <= 0 || limit > maxSearchModulesPageSize {
return searchModules{}, errors.New("limit must be between 1 and 50")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message can get out of sync with the values.

}

var query struct {
Modules []struct {
ID string `graphql:"id" json:"id,omitempty"`
Name string `graphql:"name"`
Current struct {
ID string `graphql:"id"`
Number string `graphql:"number"`
State string `graphql:"state"`
Yanked bool `graphql:"yanked"`
} `graphql:"current"`
} `graphql:"modules"`
SearchModules searchModules `graphql:"searchModules(input: $input)"`
}

if err := authenticated.Client.Query(ctx.Context, &query, map[string]interface{}{}); err != nil {
return errors.Wrap(err, "failed to query list of modules")
var after *graphql.String
if cursor != "" {
after = graphql.NewString(graphql.String(cursor))
}

sort.SliceStable(query.Modules, func(i, j int) bool {
return strings.Compare(strings.ToLower(query.Modules[i].Name), strings.ToLower(query.Modules[j].Name)) < 0
})
if err := authenticated.Client.Query(cliCtx.Context, &query, map[string]interface{}{
"input": structs.SearchInput{
First: graphql.NewInt(graphql.Int(int32(limit))), //nolint: gosec
After: after,
OrderBy: &structs.QueryOrder{
Field: "starred",
Direction: "DESC",
},
},
}); err != nil {
return searchModules{}, errors.Wrap(err, "failed to query list of modules")
}

return query.SearchModules, nil
}

func listModulesTable(modules []module) error {
columns := []string{"Name", "ID", "Current Version", "Number", "State", "Yanked"}

tableData := [][]string{columns}
for _, module := range query.Modules {
for _, module := range modules {
row := []string{
module.Name,
module.ID,
Expand All @@ -82,6 +126,15 @@ func listModulesTable(ctx *cli.Context) error {
return cmd.OutputTable(tableData, true)
}

type searchModules struct {
PageInfo structs.PageInfo `graphql:"pageInfo"`
Edges []searchModulesEdge `graphql:"edges"`
}

type searchModulesEdge struct {
Node module `graphql:"node"`
}

type module struct {
ID string `json:"id" graphql:"id"`
Administrative bool `json:"administrative" graphql:"administrative"`
Expand Down
Loading