Skip to content

Commit

Permalink
refactor: Use searchStacks over stacks for listing stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 committed Aug 28, 2024
1 parent 9da88aa commit 4f3f6d6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 71 deletions.
53 changes: 17 additions & 36 deletions internal/cmd/stack/list.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package stack

import (
"context"
"fmt"
"sort"
"github.com/spacelift-io/spacectl/client/structs"
"strings"

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

Expand All @@ -23,57 +20,41 @@ func listStacks() cli.ActionFunc {
case cmd.OutputFormatTable:
return listStacksTable(cliCtx)
case cmd.OutputFormatJSON:
return listStacksJSON(cliCtx.Context)
return listStacksJSON(cliCtx)
}

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

func listStacksJSON(ctx context.Context) error {
var query struct {
Stacks []stack `graphql:"stacks" json:"stacks,omitempty"`
func listStacksJSON(ctx *cli.Context) error {
stacks, err := searchStacks(ctx.Context, structs.SearchInput{})
if err != nil {
return err
}

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

func listStacksTable(ctx *cli.Context) error {
var query struct {
Stacks []struct {
ID string `graphql:"id" json:"id,omitempty"`
LockedBy string `graphql:"lockedBy"`
Name string `graphql:"name"`
State string `graphql:"state"`
Labels []string `graphql:"labels"`
TrackedCommit struct {
AuthorName string `graphql:"authorName"`
Hash string `graphql:"hash"`
} `graphql:"trackedCommit"`
WorkerPool struct {
Name string `graphql:"name"`
} `graphql:"workerPool"`
} `graphql:"stacks"`
}

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

sort.SliceStable(query.Stacks, func(i, j int) bool {
return strings.Compare(strings.ToLower(query.Stacks[i].Name), strings.ToLower(query.Stacks[j].Name)) < 0
stacks, err := searchStacks(ctx.Context, structs.SearchInput{
OrderBy: &structs.QueryOrder{
Field: "name",
// TODO: Make sure the order is correct
Direction: "ASC",
},
})
if err != nil {
return err
}

columns := []string{"Name", "ID", "Commit", "Author", "State", "Worker Pool", "Locked By"}
if ctx.Bool(flagShowLabels.Name) {
columns = append(columns, "Labels")
}

tableData := [][]string{columns}
for _, stack := range query.Stacks {
for _, stack := range stacks {
row := []string{
stack.Name,
stack.ID,
Expand Down
36 changes: 2 additions & 34 deletions internal/cmd/stack/open_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ type stackSearchParams struct {
branch *string
}

func searchStacks(ctx context.Context, p *stackSearchParams) ([]stack, error) {
func searchStacks(ctx context.Context, input structs.SearchInput) ([]stack, error) {
var query struct {
SearchStacksOutput struct {
Edges []struct {
Expand All @@ -172,43 +172,11 @@ func searchStacks(ctx context.Context, p *stackSearchParams) ([]stack, error) {
PageInfo structs.PageInfo `graphql:"pageInfo"`
} `graphql:"searchStacks(input: $input)"`
}
conditions := []structs.QueryPredicate{
{
Field: graphql.String("repository"),
Constraint: structs.QueryFieldConstraint{
StringMatches: &[]graphql.String{graphql.String(p.repositoryName)},
},
},
}

if p.projectRoot != nil && *p.projectRoot != "" {
root := strings.TrimSuffix(*p.projectRoot, "/")
conditions = append(conditions, structs.QueryPredicate{
Field: graphql.String("projectRoot"),
Constraint: structs.QueryFieldConstraint{
StringMatches: &[]graphql.String{graphql.String(root), graphql.String(root + "/")},
},
})
}

if p.branch != nil {
conditions = append(conditions, structs.QueryPredicate{
Field: graphql.String("branch"),
Constraint: structs.QueryFieldConstraint{
StringMatches: &[]graphql.String{graphql.String(*p.branch)},
},
})
}

variables := map[string]interface{}{"input": structs.SearchInput{
First: graphql.NewInt(graphql.Int(p.count)), //nolint: gosec
Predicates: &conditions,
}}

if err := authenticated.Client.Query(
ctx,
&query,
variables,
map[string]interface{}{"input": input},
graphql.WithHeader("Spacelift-GraphQL-Query", "StacksPage"),
); err != nil {
return nil, errors.Wrap(err, "failed search for stacks")
Expand Down
36 changes: 35 additions & 1 deletion internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stack
import (
"context"
"fmt"
"github.com/spacelift-io/spacectl/client/structs"
"strings"

"github.com/manifoldco/promptui"
Expand Down Expand Up @@ -90,7 +91,40 @@ func stackGetByID(ctx context.Context, stackID string) (*stack, error) {
}

func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (*stack, error) {
stacks, err := searchStacks(ctx, p)
conditions := []structs.QueryPredicate{
{
Field: graphql.String("repository"),
Constraint: structs.QueryFieldConstraint{
StringMatches: &[]graphql.String{graphql.String(p.repositoryName)},
},
},
}

if p.projectRoot != nil && *p.projectRoot != "" {
root := strings.TrimSuffix(*p.projectRoot, "/")
conditions = append(conditions, structs.QueryPredicate{
Field: "projectRoot",
Constraint: structs.QueryFieldConstraint{
StringMatches: &[]graphql.String{graphql.String(root), graphql.String(root + "/")},
},
})
}

if p.branch != nil {
conditions = append(conditions, structs.QueryPredicate{
Field: "branch",
Constraint: structs.QueryFieldConstraint{
StringMatches: &[]graphql.String{graphql.String(*p.branch)},
},
})
}

input := structs.SearchInput{
First: graphql.NewInt(graphql.Int(p.count)), //nolint: gosec
Predicates: &conditions,
}

stacks, err := searchStacks(ctx, input)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4f3f6d6

Please sign in to comment.