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

Check if local-preview is enabled on a stack #223

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
tomasmik marked this conversation as resolved.
Show resolved Hide resolved
}

ctx := context.Background()

var packagePath *string = nil
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/stack/open_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func findAndOpenStackInBrowser(ctx context.Context, p *stackSearchParams) error

return browser.OpenURL(authenticated.Client.URL(
"/stack/%s",
got,
got.ID,
))
}

Expand Down
53 changes: 33 additions & 20 deletions internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
stack, err := getStack(cliCtx)
if err != nil {
return "", err
}

return stack.ID, 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{
Expand All @@ -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)"`
}

Expand All @@ -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]]
Expand All @@ -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
}