Skip to content

Commit 0977af9

Browse files
authored
prompt options to use map instead of list (#351)
* prompt options to use map instead of list
1 parent 7f4c7a3 commit 0977af9

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

docs/module-definition.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ Note: Default is supplied as the starting point of the user's manual input (Not
4747
|-----------------------|-----------------|---------------------------------------------------------------------------------------------------------------------------|
4848
| `field` | string | key to store result for project definition |
4949
| `label` | string | displayed name for the prompt |
50-
| `options` | list(string) | A list of values for users to pick from |
50+
| `options` | map | A map of `value: display name` pairs for users to pick from |
5151
| `default` | string | Defaults to this value during prompt |
5252
| `value` | string | Skips prompt entirely when set |
5353
| `info` | string | Displays during prompt as extra information guiding user's input |
5454
| `fieldValidation` | Validation | Validations for the prompt value |
55-
| `type` | enum(string) | Built in custom prompts: currently supports [`AWSProfilePicker`] |
55+
| `type` | enum(string) | Built in custom prompts: currently supports [`AWSProfilePicker`] |
5656
| `execute` | string | executes commands and takes stdout as prompt result |
5757
| `omitFromProjectFile` | bool | Field is skipped from adding to project definition |
5858
| `conditions` | list(Condition) | Conditions for prompt to run, if supplied all conditions must pass |

internal/config/moduleconfig/module_config.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ type ModuleConfig struct {
2727

2828
type Parameter struct {
2929
Field string
30-
Label string `yaml:"label,omitempty"`
31-
Options []string `yaml:"options,omitempty"`
32-
Execute string `yaml:"execute,omitempty"`
33-
Value string `yaml:"value,omitempty"`
34-
Default string `yaml:"default,omitempty"`
35-
Info string `yaml:"info,omitempty"`
36-
FieldValidation Validate `yaml:"fieldValidation,omitempty"`
37-
Type string `yaml:"type,omitempty"`
38-
OmitFromProjectFile bool `yaml:"omitFromProjectFile,omitempty"`
39-
Conditions []Condition `yaml:"conditions,omitempty"`
40-
EnvVarName string `yaml:"envVarName,omitempty"`
30+
Label string `yaml:"label,omitempty"`
31+
Options yaml.MapSlice `yaml:"options,omitempty"`
32+
Execute string `yaml:"execute,omitempty"`
33+
Value string `yaml:"value,omitempty"`
34+
Default string `yaml:"default,omitempty"`
35+
Info string `yaml:"info,omitempty"`
36+
FieldValidation Validate `yaml:"fieldValidation,omitempty"`
37+
Type string `yaml:"type,omitempty"`
38+
OmitFromProjectFile bool `yaml:"omitFromProjectFile,omitempty"`
39+
Conditions []Condition `yaml:"conditions,omitempty"`
40+
EnvVarName string `yaml:"envVarName,omitempty"`
4141
}
4242

4343
type Condition struct {

internal/init/custom-prompts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func promptAWSProfilePicker(params map[string]string) error {
3636
Parameter: moduleconfig.Parameter{
3737
Field: "aws_profile",
3838
Label: "Select AWS Profile",
39-
Options: profiles,
39+
Options: listToPromptOptions(profiles),
4040
},
4141
Condition: NoCondition,
4242
Validate: NoValidation,

internal/init/prompts.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"github.com/commitdev/zero/pkg/util/exit"
1616
"github.com/commitdev/zero/pkg/util/flog"
1717
"github.com/manifoldco/promptui"
18+
"gopkg.in/yaml.v2"
1819
)
1920

20-
// Constant to maintain prompt orders so users can have the same flow,
21-
// modules get downloaded asynchronously therefore its easier to just hardcode an order
22-
var AvailableVendorOrders = []string{"aws", "github", "circleci"}
21+
const cyanArrow = "\033[36m\U000025B6\033[0m"
22+
const greenCheckMark = "\033[32m\U00002714\033[0m"
2323

2424
const awsPickProfile = "Existing AWS Profiles"
2525
const awsManualInputCredentials = "Enter my own AWS credentials"
@@ -152,12 +152,24 @@ func promptParameter(prompt PromptHandler) (error, string) {
152152
var err error
153153
var result string
154154
if len(param.Options) > 0 {
155+
var selectedIndex int
156+
// Scope of selected does not have the label data, so we need a dynamic
157+
// template with string format to put in the label in `selected`
158+
optionTemplate := &promptui.SelectTemplates{
159+
Label: `{{ . }}`,
160+
Active: fmt.Sprintf("%s {{ .Value | cyan }}", cyanArrow),
161+
Inactive: " {{ .Value }}",
162+
Selected: fmt.Sprintf("%s %s: {{ .Value }}", greenCheckMark, label),
163+
}
164+
155165
prompt := promptui.Select{
156-
Label: label,
157-
Items: param.Options,
166+
Label: label,
167+
Items: param.Options,
168+
Templates: optionTemplate,
158169
}
159-
_, result, err = prompt.Run()
160170

171+
selectedIndex, _, err = prompt.Run()
172+
result = param.Options[selectedIndex].Key.(string)
161173
} else {
162174
prompt := promptui.Prompt{
163175
Label: label,
@@ -168,7 +180,7 @@ func promptParameter(prompt PromptHandler) (error, string) {
168180
result, err = prompt.Run()
169181
}
170182
if err != nil {
171-
exit.Fatal("Exiting prompt: %v\n", err)
183+
return err, ""
172184
}
173185

174186
return nil, result
@@ -287,3 +299,14 @@ func appendToSet(set []string, toAppend []string) []string {
287299
}
288300
return set
289301
}
302+
303+
func listToPromptOptions(list []string) yaml.MapSlice {
304+
mapSlice := make(yaml.MapSlice, len(list))
305+
for i := 0; i < len(list); i++ {
306+
mapSlice[i] = yaml.MapItem{
307+
Key: list[i],
308+
Value: list[i],
309+
}
310+
}
311+
return mapSlice
312+
}

tests/test_data/modules/ci/zero-module.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ parameters:
2929
label: CI Platform
3030
# default: github
3131
options:
32-
- github
33-
- circlci
32+
github: Github
33+
circleci: Circle CI
3434
- field: circleci_api_key
3535
label: "Circle CI API Key to setup your CI/CD for repositories"
3636
conditions:
@@ -40,8 +40,8 @@ parameters:
4040
- field: useExistingAwsProfile
4141
label: "Use credentials from an existing AWS profile?"
4242
options:
43-
- "yes"
44-
- "no"
43+
"yes": "Yes"
44+
"no": "No"
4545
omitFromProjectFile: yes
4646
- field: profilePicker
4747
omitFromProjectFile: yes

0 commit comments

Comments
 (0)