Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 committed Sep 6, 2024
1 parent 810f764 commit e4b3d6e
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"github.com/urfave/cli/v2"
)

var errNoStackFound = errors.New("no stack found")
var (
errNoStackFound = errors.New("no stack found")
)

// getStackID will try to retrieve a stack ID from multiple sources.
// It will do so in the following order:
Expand All @@ -32,23 +34,27 @@ func getStackID(cliCtx *cli.Context) (string, error) {
func getStack(cliCtx *cli.Context) (*stack, error) {
if cliCtx.IsSet(flagStackID.Name) {
stackID := cliCtx.String(flagStackID.Name)
stack, err := stackGetByID(cliCtx.Context, stackID)
stack, found, err := stackGetByID(cliCtx.Context, stackID)
if err != nil {
return nil, fmt.Errorf("failed to check if stack exists: %w", err)
}
if errors.Is(err, errNoStackFound) {

if !found {
return nil, fmt.Errorf("stack with id %q could not be found. Please check that the stack exists and that you have access to it. To list available stacks run: spacectl stack list", stackID)
}

return stack, nil
} else {
if cliCtx.IsSet(flagRun.Name) {
runID := cliCtx.String(flagRun.Name)
stack, err := stackGetByRunID(cliCtx.Context, runID)
if err == nil {
return stack, nil
} else if cliCtx.IsSet(flagRun.Name) {
runID := cliCtx.String(flagRun.Name)
stack, found, err := stackGetByRunID(cliCtx.Context, runID)
if err != nil {
fmt.Printf("Failed to get stack by run id: %v\n", err)
} else {
if !found {
return nil, fmt.Errorf("run with id %q was not found. Please check that the run exists and that you have access to it. To list available stacks run: spacectl stack run list", runID)
}

// TODO: Maybe we should log error here?
return stack, nil
}
}

Expand Down Expand Up @@ -78,7 +84,7 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
return got, nil
}

func stackGetByID(ctx context.Context, stackID string) (*stack, error) {
func stackGetByID(ctx context.Context, stackID string) (*stack, bool, error) {
var query struct {
Stack struct {
stack
Expand All @@ -91,21 +97,21 @@ func stackGetByID(ctx context.Context, stackID string) (*stack, error) {

err := authenticated.Client.Query(ctx, &query, variables)
if err != nil {
return nil, fmt.Errorf("failed to query GraphQL API when checking if a stack exists: %w", err)
return nil, false, fmt.Errorf("failed to query GraphQL API when checking if a stack exists: %w", err)
}

if query.Stack.ID != stackID {
return nil, errNoStackFound
return nil, false, nil
}

return &query.Stack.stack, nil
return &query.Stack.stack, true, nil
}

func stackGetByRunID(ctx context.Context, runID string) (*stack, error) {
func stackGetByRunID(ctx context.Context, runID string) (*stack, bool, error) {
var query struct {
StackByRunId struct {
RunStack struct {
stack
} `graphql:"stackByRunId(runId: $runId)"`
} `graphql:"runStack(runId: $runId)"`
}

variables := map[string]interface{}{
Expand All @@ -114,10 +120,14 @@ func stackGetByRunID(ctx context.Context, runID string) (*stack, error) {

err := authenticated.Client.Query(ctx, &query, variables)
if err != nil {
return nil, fmt.Errorf("failed to query GraphQL API when getting stack by run id: %w", err)
if err.Error() == "not found" {
return nil, false, nil
}

return nil, false, fmt.Errorf("failed to query GraphQL API when getting stack by run id: %w", err)
}

return &query.StackByRunId.stack, nil
return &query.RunStack.stack, true, nil
}

func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (*stack, error) {
Expand Down

0 comments on commit e4b3d6e

Please sign in to comment.