Skip to content

Commit

Permalink
fix: let the caller decide what to do with the result
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Wasilewski <michal@mwasilewski.net>
  • Loading branch information
mwasilew2 committed Sep 29, 2023
1 parent 5e4b1f5 commit 8a3c9dd
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@ import (

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

type errStackWithIdNotFound struct {
stackId string
}

func (e errStackWithIdNotFound) Error() string {
return fmt.Sprintf("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", e.stackId)
}

// getStackID will try to retreive a stack ID from multiple sources.
// It will do so in the following order:
// 1. Check the --id flag, if set, use that value.
// 2. Check the current directory to determine repository and subdirectory and search for a stack.
func getStackID(cliCtx *cli.Context) (string, error) {
if cliCtx.IsSet(flagStackID.Name) {
stackId := cliCtx.String(flagStackID.Name)
err := stackExists(cliCtx.Context, stackId)
exists, err := stackExists(cliCtx.Context, stackId)
if err != nil {
return "", err
return "", fmt.Errorf("failed to check if stack exists: %w", err)
}
if !exists {
return "", 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 stackId, nil
}
Expand Down Expand Up @@ -62,7 +57,7 @@ func getStackID(cliCtx *cli.Context) (string, error) {
return got, nil
}

func stackExists(ctx context.Context, stackId string) error {
func stackExists(ctx context.Context, stackId string) (bool, error) {
var query struct {
Stack struct {
ID string `graphql:"id"`
Expand All @@ -75,14 +70,13 @@ func stackExists(ctx context.Context, stackId string) error {

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

if query.Stack.ID == "" {
return errStackWithIdNotFound{stackId: stackId}
return false, nil
}

return nil
return true, nil
}

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

0 comments on commit 8a3c9dd

Please sign in to comment.