Skip to content

Commit 4f4d36a

Browse files
authored
feat(init): prompt list of project (#3151)
1 parent b87d8b2 commit 4f4d36a

File tree

11 files changed

+339
-19
lines changed

11 files changed

+339
-19
lines changed

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ require (
99
github.com/alecthomas/assert v1.0.0
1010
github.com/buildpacks/pack v0.30.0-pre1.0.20230418191058-1190b6c52128
1111
github.com/c-bata/go-prompt v0.2.6
12+
github.com/charmbracelet/bubbletea v0.24.1
1213
github.com/chzyer/readline v1.5.1
13-
github.com/containerd/console v1.0.3
14+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81
1415
github.com/dnaeon/go-vcr v1.2.0
1516
github.com/docker/docker v24.0.2+incompatible
1617
github.com/dustin/go-humanize v1.0.1
@@ -70,6 +71,7 @@ require (
7071
github.com/aws/aws-sdk-go-v2/service/sts v1.18.9 // indirect
7172
github.com/aws/smithy-go v1.13.5 // indirect
7273
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20230322223720-077b4a917a90 // indirect
74+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
7375
github.com/buildpacks/imgutil v0.0.0-20230420161652-580610d0124b // indirect
7476
github.com/buildpacks/lifecycle v0.17.0-pre.1 // indirect
7577
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
@@ -115,6 +117,7 @@ require (
115117
github.com/kevinburke/ssh_config v1.2.0 // indirect
116118
github.com/klauspost/compress v1.16.5 // indirect
117119
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
120+
github.com/mattn/go-localereader v0.0.1 // indirect
118121
github.com/mattn/go-runewidth v0.0.14 // indirect
119122
github.com/mattn/go-tty v0.0.4 // indirect
120123
github.com/mitchellh/go-homedir v1.1.0 // indirect
@@ -124,6 +127,10 @@ require (
124127
github.com/moby/sys/signal v0.7.0 // indirect
125128
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
126129
github.com/morikuni/aec v1.0.0 // indirect
130+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
131+
github.com/muesli/cancelreader v0.2.2 // indirect
132+
github.com/muesli/reflow v0.3.0 // indirect
133+
github.com/muesli/termenv v0.15.1 // indirect
127134
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
128135
github.com/opencontainers/runc v1.1.6 // indirect
129136
github.com/opencontainers/selinux v1.11.0 // indirect

go.sum

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J
141141
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20230322223720-077b4a917a90 h1:GN8SzriwBUX5aagQft8cJ5sKUaXdHUdX8q2gS2mkom8=
142142
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20230322223720-077b4a917a90/go.mod h1:V97RBAXo2x0elgRWnSfhWLDkwwEl7dNmmBRAvbwmEoA=
143143
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
144+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
145+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
144146
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
145147
github.com/buildpacks/imgutil v0.0.0-20230420161652-580610d0124b h1:TsOLD4J7TcHQ7aaEYTDd+nEMSku3vPqq6UIyOR8IG9Q=
146148
github.com/buildpacks/imgutil v0.0.0-20230420161652-580610d0124b/go.mod h1:hgxVR7UpPvT5gATbRGM582oy048sUocDg6R6PMWAxow=
@@ -154,6 +156,8 @@ github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV
154156
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
155157
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
156158
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
159+
github.com/charmbracelet/bubbletea v0.24.1 h1:LpdYfnu+Qc6XtvMz6d/6rRY71yttHTP5HtrjMgWvixc=
160+
github.com/charmbracelet/bubbletea v0.24.1/go.mod h1:rK3g/2+T8vOSEkNHvtq40umJpeVYDn6bLaqbgzhL/hg=
157161
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589 h1:krfRl01rzPzxSxyLyrChD+U+MzsBXbm0OwYYB67uF+4=
158162
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589/go.mod h1:OuDyvmLnMCwa2ep4Jkm6nyA0ocJuZlGyk2gGseVzERM=
159163
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
@@ -177,8 +181,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
177181
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
178182
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
179183
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
180-
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
181-
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
184+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
185+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
182186
github.com/containerd/containerd v1.7.0 h1:G/ZQr3gMZs6ZT0qPUZ15znx5QSdQdASW11nXTLTM2Pg=
183187
github.com/containerd/containerd v1.7.0/go.mod h1:QfR7Efgb/6X2BDpTPJRvPTYDE9rsF0FsXX9J8sIs/sc=
184188
github.com/containerd/continuity v0.3.1-0.20230206214859-2a963a2f56e8 h1:EdSQb65ohzz4jsyPOhxfu3/+c9nnU0euk0otferwl9A=
@@ -412,9 +416,12 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
412416
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
413417
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
414418
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
419+
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
420+
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
415421
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
416422
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
417423
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
424+
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
418425
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
419426
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
420427
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
@@ -441,6 +448,14 @@ github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbD
441448
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
442449
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
443450
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
451+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
452+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
453+
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
454+
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
455+
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
456+
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
457+
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
458+
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
444459
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
445460
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
446461
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
@@ -471,6 +486,7 @@ github.com/quantumsheep/pack v0.30.0-pre1.0.20230505095233-37158bb223a4 h1:1NPuV
471486
github.com/quantumsheep/pack v0.30.0-pre1.0.20230505095233-37158bb223a4/go.mod h1:JFPC0K0VzwZVW78VPN/CDD34lhTIpdAlR/zu+WhpnnM=
472487
github.com/rivo/tview v0.0.0-20230406072732-e22ce9588bb4 h1:zX+lRcFRPX1jn8A11jxT0dEQhkmUM7pec+9NLK8MiTQ=
473488
github.com/rivo/tview v0.0.0-20230406072732-e22ce9588bb4/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
489+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
474490
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
475491
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
476492
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=

internal/core/testing.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,10 @@ type GoldenReplacement struct {
599599
// This is the format for repl in (*regexp.Regexp).ReplaceAll
600600
// You can use $ to represent groups $1, $2...
601601
Replacement string
602+
603+
// OptionalMatch allow the golden to not contain the given patterns
604+
// if false, the golden must contain the given pattern
605+
OptionalMatch bool
602606
}
603607

604608
// goldenReplacePatterns replace the list of patterns with their given replacement
@@ -608,7 +612,9 @@ func goldenReplacePatterns(golden string, replacements ...GoldenReplacement) (st
608612

609613
for _, replacement := range replacements {
610614
if !replacement.Pattern.MatchString(changedGolden) {
611-
matchFailed = append(matchFailed, replacement.Pattern.String())
615+
if !replacement.OptionalMatch {
616+
matchFailed = append(matchFailed, replacement.Pattern.String())
617+
}
612618
continue
613619
}
614620
changedGolden = replacement.Pattern.ReplaceAllString(changedGolden, replacement.Replacement)

internal/interactive/list.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//go:build !wasm
2+
3+
package interactive
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"os"
9+
10+
tea "github.com/charmbracelet/bubbletea"
11+
)
12+
13+
type ListPrompt struct {
14+
// Prompt that will be printed when showing the list
15+
Prompt string
16+
Choices []string
17+
// DefaultIndex is the element that will be selected when starting prompt
18+
DefaultIndex int
19+
20+
cursor int
21+
cancelled bool
22+
}
23+
24+
func (m *ListPrompt) Init() tea.Cmd {
25+
return nil
26+
}
27+
28+
func (m *ListPrompt) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
29+
switch msg := msg.(type) {
30+
// Key is pressed
31+
case tea.KeyMsg:
32+
switch msg.String() {
33+
case "ctrl+c", "q":
34+
m.cancelled = true
35+
return m, tea.Quit
36+
case "up", "k":
37+
if m.cursor > 0 {
38+
m.cursor--
39+
}
40+
case "down", "j":
41+
if m.cursor < len(m.Choices)-1 {
42+
m.cursor++
43+
}
44+
case "enter", " ":
45+
return m, tea.Quit
46+
}
47+
}
48+
49+
return m, nil
50+
}
51+
52+
func (m *ListPrompt) View() string {
53+
s := m.Prompt + "\n\n"
54+
55+
for i, choice := range m.Choices {
56+
if m.cursor == i {
57+
s += fmt.Sprintf("> %s\n", choice)
58+
} else {
59+
s += fmt.Sprintf("%s\n", choice)
60+
}
61+
}
62+
63+
s += "\nPress enter or space for select.\n"
64+
65+
return s
66+
}
67+
68+
// Execute start the prompt and return the selected index
69+
func (m *ListPrompt) Execute(ctx context.Context) (int, error) {
70+
m.cursor = m.DefaultIndex
71+
72+
opts := []tea.ProgramOption{
73+
tea.WithContext(ctx),
74+
}
75+
76+
if hasMockedResponse(ctx) {
77+
opts = append(opts, tea.WithInput(&mockResponseReader{
78+
ctx: ctx,
79+
defaultReader: os.Stdin,
80+
}))
81+
}
82+
83+
p := tea.NewProgram(m, opts...)
84+
_, err := p.Run()
85+
if err != nil {
86+
return -1, fmt.Errorf("error running prompt: %w", err)
87+
}
88+
89+
if m.cancelled {
90+
return -1, fmt.Errorf("prompt cancelled")
91+
}
92+
93+
return m.cursor, nil
94+
}

internal/interactive/list_disabled.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build wasm
2+
3+
package interactive
4+
5+
import (
6+
"context"
7+
"fmt"
8+
)
9+
10+
type ListPrompt struct {
11+
Prompt string
12+
Choices []string
13+
DefaultIndex int
14+
}
15+
16+
func (m *ListPrompt) Execute(ctx context.Context) (int, error) {
17+
return -1, fmt.Errorf("not implemented for current platform")
18+
}

internal/interactive/test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func popMockResponseFromContext(ctx context.Context) (string, bool) {
3333
return str, true
3434
}
3535

36+
func hasMockedResponse(ctx context.Context) bool {
37+
return ctx.Value(contextKey) != nil
38+
}
39+
3640
type mockResponseReader struct {
3741
ctx context.Context
3842
defaultReader io.ReadCloser

internal/namespaces/init/init.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ Default path for configuration file is based on the following priority order:
170170

171171
if args.ProjectID == "" {
172172
args.ProjectID = getAPIKeyDefaultProjectID(ctx, args.AccessKey, args.SecretKey)
173-
}
174-
175-
if args.ProjectID == "" {
176-
args.ProjectID, err = promptProjectID(ctx)
173+
args.ProjectID, err = promptProjectID(ctx, args.AccessKey, args.SecretKey, args.OrganizationID, args.ProjectID)
177174
if err != nil {
178175
return nil, err
179176
}

internal/namespaces/init/init_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package init
33
import (
44
"fmt"
55
"path"
6+
"regexp"
67
"testing"
78

89
"github.com/alecthomas/assert"
@@ -198,3 +199,48 @@ func TestInit(t *testing.T) {
198199
}))
199200
})
200201
}
202+
203+
func TestInit_Prompt(t *testing.T) {
204+
promptResponse := []string{
205+
"secret-key",
206+
"access-key",
207+
"organization-id",
208+
" ",
209+
}
210+
211+
t.Run("Simple", core.Test(&core.TestConfig{
212+
Commands: GetCommands(),
213+
BeforeFunc: core.BeforeFuncCombine(
214+
baseBeforeFunc(),
215+
func(ctx *core.BeforeFuncCtx) error {
216+
promptResponse[0] = ctx.Meta["SecretKey"].(string)
217+
promptResponse[1] = ctx.Meta["AccessKey"].(string)
218+
promptResponse[2] = ctx.Meta["OrganizationID"].(string)
219+
220+
return nil
221+
}),
222+
TmpHomeDir: true,
223+
Cmd: "scw init",
224+
Check: core.TestCheckCombine(
225+
core.TestCheckGoldenAndReplacePatterns(
226+
core.GoldenReplacement{
227+
Pattern: regexp.MustCompile("\\s\\sExcept for autocomplete: unsupported OS 'windows'\n"),
228+
Replacement: "",
229+
OptionalMatch: true,
230+
},
231+
core.GoldenReplacement{
232+
Pattern: regexp.MustCompile(`Except for autocomplete: unsupported OS 'windows'\\n`),
233+
Replacement: "",
234+
OptionalMatch: true,
235+
},
236+
),
237+
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
238+
secretKey, _ := ctx.Client.GetSecretKey()
239+
assert.Equal(t, secretKey, *config.SecretKey)
240+
assert.NotEmpty(t, *config.DefaultProjectID)
241+
assert.Equal(t, *config.DefaultProjectID, *config.DefaultProjectID)
242+
}),
243+
),
244+
PromptResponseMocks: promptResponse,
245+
}))
246+
}

internal/namespaces/init/prompt.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"fmt"
66

77
"github.com/fatih/color"
8+
"github.com/scaleway/scaleway-cli/v2/internal/core"
89
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
910
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
11+
"github.com/scaleway/scaleway-sdk-go/api/account/v2"
1012
"github.com/scaleway/scaleway-sdk-go/logger"
1113
"github.com/scaleway/scaleway-sdk-go/scw"
1214
"github.com/scaleway/scaleway-sdk-go/validation"
@@ -26,18 +28,40 @@ func promptOrganizationID(ctx context.Context) (string, error) {
2628
})
2729
}
2830

29-
func promptProjectID(ctx context.Context) (string, error) {
31+
func promptProjectID(ctx context.Context, accessKey string, secretKey string, organizationID string, defaultProjectID string) (string, error) {
32+
client := core.ExtractClient(ctx)
33+
api := account.NewAPI(client)
34+
35+
res, err := api.ListProjects(&account.ListProjectsRequest{
36+
OrganizationID: organizationID,
37+
}, scw.WithAllPages(), scw.WithContext(ctx), scw.WithAuthRequest(accessKey, secretKey))
38+
if err != nil {
39+
return "", fmt.Errorf("failed to list projects: %w", err)
40+
}
41+
42+
defaultIndex := 0
43+
44+
projects := make([]string, len(res.Projects))
45+
for i := range res.Projects {
46+
if res.Projects[i].ID == defaultProjectID {
47+
defaultIndex = i
48+
}
49+
projects[i] = fmt.Sprintf("%s (%s)", res.Projects[i].Name, res.Projects[i].ID)
50+
}
51+
52+
prompt := interactive.ListPrompt{
53+
Prompt: "Choose your default project ID",
54+
Choices: projects,
55+
DefaultIndex: defaultIndex,
56+
}
57+
3058
_, _ = interactive.Println()
31-
return interactive.PromptStringWithConfig(&interactive.PromptStringConfig{
32-
Ctx: ctx,
33-
Prompt: "Default project ID",
34-
ValidateFunc: func(s string) error {
35-
if !validation.IsUUID(s) {
36-
return fmt.Errorf("given project ID is not a valid UUID")
37-
}
38-
return nil
39-
},
40-
})
59+
index, err := prompt.Execute(ctx)
60+
if err != nil {
61+
return "", err
62+
}
63+
64+
return res.Projects[index].ID, nil
4165
}
4266

4367
func promptTelemetry(ctx context.Context) (*bool, error) {

0 commit comments

Comments
 (0)