diff --git a/internal/cmd/stack/local_preview.go b/internal/cmd/stack/local_preview.go index 7431ab3..eeffc11 100644 --- a/internal/cmd/stack/local_preview.go +++ b/internal/cmd/stack/local_preview.go @@ -22,11 +22,16 @@ func localPreview() cli.ActionFunc { return err } - stackID, err := getStackID(cliCtx) + stack, err := getStack(cliCtx) if err != nil { return err } + if !stack.LocalPreviewEnabled { + linkToStack := authenticated.Client.URL("/stack/%s", stack.ID) + return fmt.Errorf("local preview is not enabled for this stack, enable it in the stack settings: %s", linkToStack) + } + ctx := context.Background() var packagePath *string = nil @@ -57,7 +62,7 @@ func localPreview() cli.ActionFunc { } uploadVariables := map[string]interface{}{ - "stack": graphql.ID(stackID), + "stack": graphql.ID(stack.ID), } if err := authenticated.Client.Mutate(ctx, &uploadMutation, uploadVariables); err != nil { @@ -104,7 +109,7 @@ func localPreview() cli.ActionFunc { } triggerVariables := map[string]interface{}{ - "stack": graphql.ID(stackID), + "stack": graphql.ID(stack.ID), "workspace": graphql.ID(uploadMutation.UploadLocalWorkspace.ID), "environmentVarsOverrides": envVars, } @@ -122,7 +127,7 @@ func localPreview() cli.ActionFunc { linkToRun := authenticated.Client.URL( "/stack/%s/run/%s", - stackID, + stack.ID, triggerMutation.RunProposeLocalWorkspace.ID, ) fmt.Println("The live run can be visited at", linkToRun) @@ -131,7 +136,7 @@ func localPreview() cli.ActionFunc { return nil } - terminal, err := runLogsWithAction(ctx, stackID, triggerMutation.RunProposeLocalWorkspace.ID, nil) + terminal, err := runLogsWithAction(ctx, stack.ID, triggerMutation.RunProposeLocalWorkspace.ID, nil) if err != nil { return err } diff --git a/internal/cmd/stack/open_command.go b/internal/cmd/stack/open_command.go index 76c3e6b..5d0df5b 100644 --- a/internal/cmd/stack/open_command.go +++ b/internal/cmd/stack/open_command.go @@ -68,7 +68,7 @@ func findAndOpenStackInBrowser(ctx context.Context, p *stackSearchParams) error return browser.OpenURL(authenticated.Client.URL( "/stack/%s", - got, + got.ID, )) } diff --git a/internal/cmd/stack/stack_selector.go b/internal/cmd/stack/stack_selector.go index 4fd8512..bf8085d 100644 --- a/internal/cmd/stack/stack_selector.go +++ b/internal/cmd/stack/stack_selector.go @@ -20,26 +20,35 @@ var errNoStackFound = errors.New("no stack found") // 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) { + stackID, err := getStackID(cliCtx) + if err != nil { + return "", err + } + + return stackID, nil +} + +func getStack(cliCtx *cli.Context) (*stack, error) { if cliCtx.IsSet(flagStackID.Name) { stackID := cliCtx.String(flagStackID.Name) - exists, err := stackExists(cliCtx.Context, stackID) + stack, err := stackGetByID(cliCtx.Context, stackID) if err != nil { - return "", fmt.Errorf("failed to check if stack exists: %w", err) + return nil, 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) + if errors.Is(err, errNoStackFound) { + 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 stackID, nil + return stack, nil } subdir, err := getGitRepositorySubdir() if err != nil { - return "", err + return nil, err } name, err := getRepositoryName() if err != nil { - return "", err + return nil, err } got, err := findAndSelectStack(cliCtx.Context, &stackSearchParams{ @@ -49,19 +58,19 @@ func getStackID(cliCtx *cli.Context) (string, error) { }, true) if err != nil { if errors.Is(err, errNoStackFound) { - return "", fmt.Errorf("%w: no --id flag was provided and stack could not be found by searching the current directory", err) + return nil, fmt.Errorf("%w: no --id flag was provided and stack could not be found by searching the current directory", err) } - return "", err + return nil, err } return got, nil } -func stackExists(ctx context.Context, stackID string) (bool, error) { +func stackGetByID(ctx context.Context, stackID string) (*stack, error) { var query struct { Stack struct { - ID string `graphql:"id"` + stack } `graphql:"stack(id: $id)"` } @@ -71,27 +80,31 @@ func stackExists(ctx context.Context, stackID string) (bool, error) { 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) + return nil, fmt.Errorf("failed to query GraphQL API when checking if a stack exists: %w", err) } - return query.Stack.ID != "", nil + if query.Stack.ID != stackID { + return nil, errNoStackFound + } + + return &query.Stack.stack, nil } -func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (string, error) { +func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (*stack, error) { stacks, err := searchStacks(ctx, p) if err != nil { - return "", err + return nil, err } items := []string{} - found := map[string]string{} + found := map[string]stack{} for _, stack := range stacks { items = append(items, stack.Name) - found[stack.Name] = stack.ID + found[stack.Name] = stack } if len(found) == 0 { - return "", errNoStackFound + return nil, errNoStackFound } selected := found[items[0]] @@ -112,11 +125,11 @@ func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt b _, result, err := prompt.Run() if err != nil { - return "", err + return nil, err } selected = found[result] } - return selected, nil + return &selected, nil }