Repository: jesseduffield/lazygit
File: pkg/gui/custom_commands.go (Go)

Code before:
					// Collect cmd to run from config
					cmdStr, err := gui.resolveTemplate(prompt.Command, promptResponses)
					if err != nil {
						return gui.surfaceError(err)
					}

					// Collect Filter regexp
					filter, err := gui.resolveTemplate(prompt.Filter, promptResponses)
					if err != nil {
						return gui.surfaceError(err)
					}
					reg, err := regexp.Compile(filter)
					if err != nil {
						return gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
					}

					// Run and save output
					message, err := gui.GitCommand.RunCommandWithOutput(cmdStr)
					if err != nil {
						return gui.surfaceError(err)
					}

					// Need to make a menu out of what the cmd has displayed
					candidates := []string{}
					buff := bytes.NewBuffer(nil)
					temp := template.Must(template.New("format").Parse(prompt.Format))
					for _, str := range strings.Split(string(message), "\n") {
						if str == "" {
							continue
						}
						tmplData := map[string]string{}
						out := reg.FindAllStringSubmatch(str, -1)
						if len(out) > 0 {
							for groupIdx, group := range reg.SubexpNames() {
								// Record matched group with group ids
								matchName := "group_" + strconv.Itoa(groupIdx)
								tmplData[matchName] = group
								// Record last named group non-empty matches as group matches
								if group != "" {
									tmplData[group] = out[0][idx]
								}
							}
						}
						temp.Execute(buff, tmplData)
						candidates = append(candidates, strings.TrimSpace(buff.String()))
						buff.Reset()
					}

					menuItems := make([]*menuItem, len(candidates))
					for i := range candidates {
						menuItems[i] = &menuItem{

Review comment (suggestion):
Can you remove the `template.Must` wrapper? Just like the filter this is user specific and so could cause errors

Code after:
	if err != nil {
		return gui.surfaceError(err)
	}

	// Collect Filter regexp
	filter, err := gui.resolveTemplate(prompt.Filter, promptResponses)
	if err != nil {
		return gui.surfaceError(err)
	}

	// Run and save output
	message, err := gui.GitCommand.RunCommandWithOutput(cmdStr)
	if err != nil {
		return gui.surfaceError(err)
	}

	// Need to make a menu out of what the cmd has displayed
	candidates, err := gui.GenerateMenuCandidates(message, filter, prompt.Format)
	if err != nil {
		return gui.surfaceError(err)
	}

	menuItems := make([]*menuItem, len(candidates))
	for i := range candidates {
		menuItems[i] = &menuItem{
			displayStrings: []string{candidates[i]},
			onPress: func() error {
				promptResponses[responseIdx] = candidates[i]
				return wrappedF()
			},
		}
	}

	title, err := gui.resolveTemplate(prompt.Title, promptResponses)
	if err != nil {
		return gui.surfaceError(err)
	}

	return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
}

func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand) func() error {
	return func() error {
		promptResponses := make([]string, len(customCommand.Prompts))

		f := func() error {
			cmdStr, err := gui.resolveTemplate(customCommand.Command, promptResponses)
			if err != nil {
				return gui.surfaceError(err)
			}