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

Improve user facing error massage when the stack is not found #182

2 changes: 1 addition & 1 deletion internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func localPreview() cli.ActionFunc {
}

if err := authenticated.Client.Mutate(ctx, &uploadMutation, uploadVariables); err != nil {
return err
return fmt.Errorf("failed to upload local workspace: %w", err)
}

fp := filepath.Join(os.TempDir(), "spacectl", "local-workspace", fmt.Sprintf("%s.tar.gz", uploadMutation.UploadLocalWorkspace.ID))
Expand Down
34 changes: 33 additions & 1 deletion internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
mwasilew2 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/urfave/cli/v2"
)

Expand All @@ -18,7 +20,15 @@ var errNoStackFound = errors.New("no stack found")
// 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) {
return cliCtx.String(flagStackID.Name), nil
stackId := cliCtx.String(flagStackID.Name)
exists, err := stackExists(cliCtx.Context, stackId)
if err != nil {
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
}

subdir, err := getGitRepositorySubdir()
Expand Down Expand Up @@ -47,6 +57,28 @@ func getStackID(cliCtx *cli.Context) (string, error) {
return got, nil
}

func stackExists(ctx context.Context, stackId string) (bool, error) {
var query struct {
Stack struct {
ID string `graphql:"id"`
} `graphql:"stack(id: $id)"`
}

variables := map[string]interface{}{
"id": graphql.ID(stackId),
}

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

if query.Stack.ID == "" {
mwasilew2 marked this conversation as resolved.
Show resolved Hide resolved
return false, nil
}
return true, nil
}

func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (string, error) {
stacks, err := searchStacks(ctx, p)
if err != nil {
Expand Down