-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.go
More file actions
90 lines (77 loc) · 2.39 KB
/
selector.go
File metadata and controls
90 lines (77 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package ui provides interactive selection prompts for the CLI.
package ui
import (
"errors"
"fmt"
"os"
"sort"
"strings"
"github.com/Iilun/survey/v2"
"github.com/aaearon/grant-cli/internal/sca/models"
)
// FormatTargetOption formats an eligible target into a display string.
func FormatTargetOption(target models.EligibleTarget) string {
var prefix string
switch strings.ToLower(string(target.WorkspaceType)) {
case "subscription":
prefix = "Subscription"
case "resource_group":
prefix = "Resource Group"
case "management_group":
prefix = "Management Group"
case "directory":
prefix = "Directory"
case "resource":
prefix = "Resource"
case "account":
prefix = "Account"
default:
prefix = string(target.WorkspaceType)
}
base := fmt.Sprintf("%s: %s / Role: %s", prefix, target.WorkspaceName, target.RoleInfo.Name)
if target.CSP != "" {
return fmt.Sprintf("%s (%s)", base, strings.ToLower(string(target.CSP)))
}
return base
}
// BuildOptions builds a sorted list of display options from eligible targets.
func BuildOptions(targets []models.EligibleTarget) []string {
if len(targets) == 0 {
return []string{}
}
options := make([]string, len(targets))
for i, target := range targets {
options[i] = FormatTargetOption(target)
}
sort.Strings(options)
return options
}
// FindTargetByDisplay finds a target by its formatted display string.
func FindTargetByDisplay(targets []models.EligibleTarget, display string) (*models.EligibleTarget, error) {
for i := range targets {
if FormatTargetOption(targets[i]) == display {
return &targets[i], nil
}
}
return nil, fmt.Errorf("target not found: %s", display)
}
// SelectTarget presents an interactive selector for choosing a target.
func SelectTarget(targets []models.EligibleTarget) (*models.EligibleTarget, error) {
if !IsInteractive() {
return nil, fmt.Errorf("%w; use --target and --role flags for non-interactive mode", ErrNotInteractive)
}
if len(targets) == 0 {
return nil, errors.New("no eligible targets available")
}
options := BuildOptions(targets)
var selected string
prompt := &survey.Select{
Message: "Select a target:",
Options: options,
Filter: nil, // Enable default fuzzy filter
}
if err := survey.AskOne(prompt, &selected, survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)); err != nil {
return nil, fmt.Errorf("target selection failed: %w", err)
}
return FindTargetByDisplay(targets, selected)
}