From f77a26f54c8ac204e99771336bbe1bd0b1605f60 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Wed, 13 Nov 2024 10:05:17 +0000
Subject: [PATCH] chore(cli): exp prompt-example: add option to multi-select

---
 cli/prompts.go | 50 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 18 deletions(-)

diff --git a/cli/prompts.go b/cli/prompts.go
index e550e591d1a19..9bd7ecaa03204 100644
--- a/cli/prompts.go
+++ b/cli/prompts.go
@@ -22,14 +22,26 @@ func (RootCmd) promptExample() *serpent.Command {
 		}
 	}
 
-	var useSearch bool
-	useSearchOption := serpent.Option{
-		Name:        "search",
-		Description: "Show the search.",
-		Required:    false,
-		Flag:        "search",
-		Value:       serpent.BoolOf(&useSearch),
-	}
+	var (
+		useSearch       bool
+		useSearchOption = serpent.Option{
+			Name:        "search",
+			Description: "Show the search.",
+			Required:    false,
+			Flag:        "search",
+			Value:       serpent.BoolOf(&useSearch),
+		}
+
+		multiSelectValues []string
+		multiSelectError  error
+		useThingsOption   = serpent.Option{
+			Name:        "things",
+			Description: "Tell me what things you want.",
+			Flag:        "things",
+			Default:     "",
+			Value:       serpent.StringArrayOf(&multiSelectValues),
+		}
+	)
 	cmd := &serpent.Command{
 		Use:   "prompt-example",
 		Short: "Example of various prompt types used within coder cli.",
@@ -140,16 +152,18 @@ func (RootCmd) promptExample() *serpent.Command {
 				return err
 			}),
 			promptCmd("multi-select", func(inv *serpent.Invocation) error {
-				values, err := cliui.MultiSelect(inv, cliui.MultiSelectOptions{
-					Message: "Select some things:",
-					Options: []string{
-						"Code", "Chair", "Whale", "Diamond", "Carrot",
-					},
-					Defaults: []string{"Code"},
-				})
-				_, _ = fmt.Fprintf(inv.Stdout, "%q are nice choices.\n", strings.Join(values, ", "))
-				return err
-			}),
+				if len(multiSelectValues) == 0 {
+					multiSelectValues, multiSelectError = cliui.MultiSelect(inv, cliui.MultiSelectOptions{
+						Message: "Select some things:",
+						Options: []string{
+							"Code", "Chair", "Whale", "Diamond", "Carrot",
+						},
+						Defaults: []string{"Code"},
+					})
+				}
+				_, _ = fmt.Fprintf(inv.Stdout, "%q are nice choices.\n", strings.Join(multiSelectValues, ", "))
+				return multiSelectError
+			}, useThingsOption),
 			promptCmd("rich-parameter", func(inv *serpent.Invocation) error {
 				value, err := cliui.RichSelect(inv, cliui.RichSelectOptions{
 					Options: []codersdk.TemplateVersionParameterOption{