Skip to content

Commit

Permalink
Allow auto-select for stackID when only finding one stack (#265)
Browse files Browse the repository at this point in the history
* Allow auto-select for stackID when only finding one stack

```
➜  empty git:(diff) ✗ /Users/tomas/spacelift-io/spacectl/build/aa stack show
Enable auto-selection by setting SPACECTL_SKIP_STACK_PROMPT=true
Use the arrow keys to navigate: ↓ ↑ → ←  and / toggles search
? Found 1 stacks, select one:
  ▸ doublestack
2024/09/27 14:49:35 ^C
➜  empty git:(diff) ✗ SPACECTL_SKIP_STACK_PROMPT=true
➜  empty git:(diff) ✗ /Users/tomas/spacelift-io/spacectl/build/aa stack show

Provider   | GitHub
Repository | empty
Branch     | master
```
  • Loading branch information
tomasmik authored Oct 8, 2024
1 parent 2c160bc commit 48da190
Showing 1 changed file with 11 additions and 1 deletion.
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

0 comments on commit 48da190

Please sign in to comment.