Skip to content

Add executeCommand Prompt #4183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion pkg/gui/services/custom_commands/handler_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,32 @@ 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'")
}
}

return f()
}
}

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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down