Add condition field to custom command prompts#5364
Add condition field to custom command prompts#5364mrt181 wants to merge 1 commit intojesseduffield:masterfrom
Conversation
cfd9c66 to
e934c7c
Compare
e934c7c to
4040756
Compare
78c0266 to
a51be39
Compare
When building multi-step custom command forms, some prompts are only relevant depending on earlier answers. Without conditional logic, users must dismiss irrelevant prompts manually. Prompts now accept a `condition` field with a template expression evaluated against prior form values. Skipped prompts default to an empty string.
a51be39 to
b117f12
Compare
stefanhaller
left a comment
There was a problem hiding this comment.
Thanks, looks reasonable overall. A few things below.
| if resolved { | ||
| return showPrompt() | ||
| } | ||
| if _, exists := form[prompt.Key]; !exists { |
There was a problem hiding this comment.
Why do we need the !exists condition here? How can form[prompt.Key] be set when we get here, and why would we want to keep its value in that case?
| expr = strings.TrimPrefix(expr, "{{") | ||
| expr = strings.TrimSuffix(expr, "}}") | ||
| } | ||
| ifTemplate := fmt.Sprintf("{{ if %s }}1{{ end }}", expr) |
There was a problem hiding this comment.
I'm not an expert at go templates, but this looks really hacky to me. Isn't there a cleaner way to evaluate a boolean template expression?
| | type | One of 'input', 'confirm', 'menu', 'menuFromCommand' | yes | | ||
| | title | The title to display in the popup panel | no | | ||
| | key | Used to reference the entered value from within the custom command. E.g. a prompt with `key: 'Branch'` can be referred to as `{{.Form.Branch}}` in the command | yes | | ||
| | condition | A Go template expression; if it resolves to empty string or `false`, the prompt is skipped. See [Conditional prompts](#conditional-prompts) | no | |
There was a problem hiding this comment.
All changes in this file need to be moved to docs-master.
| }, | ||
| "condition": { | ||
| "type": "string", | ||
| "description": "A Go template expression evaluated against the current form state. If it resolves to empty string or 'false', the prompt is skipped.", | ||
| "examples": [ | ||
| "{{ eq .Form.Choice \"yes\" }}" | ||
| ] |
There was a problem hiding this comment.
This shouldn't be here, we only want to touch schema-master/config.json. Also, we never manually edit it; if you want examples to appear in the schema, you need to add jsonschema:"example=..." to the entry in user_config.go.
When building multi-step custom command forms, some prompts are only relevant depending on earlier answers. Without conditional logic, users must dismiss irrelevant prompts manually.
Prompts now accept a
conditionfield with a template expression evaluated against prior form values. Skipped prompts default to an empty string.PR Description
Please check if the PR fulfills these requirements
go generate ./...)