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

Allow auto-select for stackID when only finding one stack #265

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Changes from all commits
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
12 changes: 11 additions & 1 deletion internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stack
import (
"context"
"fmt"
"os"
"strings"

"github.com/manifoldco/promptui"
Expand All @@ -17,6 +18,10 @@ var (
errNoStackFound = errors.New("no stack found")
)

const (
envPromptSkipKey = "SPACECTL_SKIP_STACK_PROMPT"
)

// getStackID will try to retrieve a stack ID from multiple sources.
// It will do so in the following order:
// 1. Check the --id flag, if set, use that value.
Expand Down Expand Up @@ -66,11 +71,13 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
return nil, err
}

skip := os.Getenv(envPromptSkipKey) == "true"

got, err := findAndSelectStack(cliCtx.Context, &stackSearchParams{
count: 50,
projectRoot: &subdir,
repositoryName: name,
}, true)
}, !skip)
if err != nil {
if errors.Is(err, errNoStackFound) {
return nil, fmt.Errorf("%w: no --id flag was provided and stack could not be found by searching the current directory", err)
Expand Down Expand Up @@ -183,6 +190,9 @@ func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt b
if len(items) == p.count {
fmt.Printf("Search results exceeded maximum capacity (%d) some stacks might be missing\n", p.count)
}
if len(items) == 1 && forcePrompt {
fmt.Printf("Enable auto-selection by setting '%s=true'\n", envPromptSkipKey)
}

prompt := promptui.Select{
Label: fmt.Sprintf("Found %d stacks, select one", len(items)),
Expand Down
Loading