Skip to content

Commit

Permalink
Check if local-preview is enabled on a stack
Browse files Browse the repository at this point in the history
➜  test git:(main) ✗ spc stack local-preview
✔ subdir
2024/03/15 13:38:12 local preview is not enabled for this stack, enable it in the stack settings: http://tomasmik.app.spacelift.tf/stack/subdir
  • Loading branch information
tomasmik committed Mar 15, 2024
1 parent ba45e16 commit 8c1d575
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
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)
}

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) {
stackID, err := getStackID(cliCtx)

Check failure on line 23 in internal/cmd/stack/stack_selector.go

View workflow job for this annotation

GitHub Actions / Lint the code

SA5007: infinite recursive call (staticcheck)
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{
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
}

0 comments on commit 8c1d575

Please sign in to comment.