-
Notifications
You must be signed in to change notification settings - Fork 35
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
@@ -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 | ||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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"` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.