feat: add executeCommand Prompt

This commit is contained in:
Mara Kalwa 2025-01-18 22:55:49 +01:00
parent c03b892270
commit f7b8b428ae
3 changed files with 25 additions and 3 deletions

View file

@ -634,7 +634,7 @@ type CustomCommand struct {
}
type CustomCommandPrompt struct {
// One of: 'input' | 'menu' | 'confirm' | 'menuFromCommand'
// One of: 'input' | 'menu' | 'confirm' | 'menuFromCommand' | 'executeCommand'
Type string `yaml:"type"`
// 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
Key string `yaml:"key"`
@ -657,7 +657,8 @@ type CustomCommandPrompt struct {
Options []CustomCommandMenuOption `yaml:"options"`
// The command to run to generate menu options
// Only for menuFromCommand prompts.
// or run by executeCommand
// Only for menuFromCommand prompts and executeCommand prompts.
Command string `yaml:"command" jsonschema:"example=git fetch {{.Form.Remote}} {{.Form.Branch}} && git checkout FETCH_HEAD"`
// The regexp to run specifying groups which are going to be kept from the command's output.
// Only for menuFromCommand prompts.

View file

@ -103,8 +103,16 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro
}
return self.confirmPrompt(resolvedPrompt, g)
}
case "executeCommand":
f = func() error {
resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate)
if err != nil {
return err
}
return self.executeCommand(resolvedPrompt, wrappedF)
}
default:
return errors.New("custom command prompt must have a type of 'input', 'menu', 'menuFromCommand', or 'confirm'")
return errors.New("custom command prompt must have a type of 'input', 'menu', 'menuFromCommand', 'executeCommand' or 'confirm'")
}
}
@ -112,6 +120,15 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro
}
}
func (self *HandlerCreator) executeCommand(prompt *config.CustomCommandPrompt, wrappedF func(string) error) error {
output, err := self.c.OS().Cmd.NewShell(prompt.Command).RunWithOutput()
if err != nil {
return err
}
return wrappedF(output)
}
func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrappedF func(string) error) error {
findSuggestionsFn, err := self.generateFindSuggestionsFunc(prompt)
if err != nil {

View file

@ -186,6 +186,10 @@ type PromptOpts struct {
Mask bool
}
type ExecuteCommandOpts struct {
Command string
}
type MenuSection struct {
Title string
Column int // The column that this section title should be aligned with