From f7b8b428ae85cb48265df0ea20d786aa290c9979 Mon Sep 17 00:00:00 2001 From: Mara Kalwa Date: Sat, 18 Jan 2025 22:55:49 +0100 Subject: [PATCH] feat: add executeCommand Prompt --- pkg/config/user_config.go | 5 +++-- .../custom_commands/handler_creator.go | 19 ++++++++++++++++++- pkg/gui/types/common.go | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 3df5c5a9b..282d29c5b 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -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. diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index 95de40a2e..48b89cebe 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -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 { diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 82a309fb1..7524d4a33 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -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