Skip to content

Commit b5da6c4

Browse files
committed
fixup! Add runCommand function to Go template syntax
Share the "runCommand" handler.
1 parent d76abb1 commit b5da6c4

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

pkg/commands/git_commands/custom.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package git_commands
22

3-
import "github.com/mgutz/str"
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/mgutz/str"
8+
)
49

510
type CustomCommands struct {
611
*GitCommon
@@ -18,3 +23,18 @@ func NewCustomCommands(gitCommon *GitCommon) *CustomCommands {
1823
func (self *CustomCommands) RunWithOutput(cmdStr string) (string, error) {
1924
return self.cmd.New(str.ToArgv(cmdStr)).RunWithOutput()
2025
}
26+
27+
// A function that can be used as a "runCommand" entry in the template.FuncMap of templates.
28+
func (self *CustomCommands) TemplateFunctionRunCommand(cmdStr string) (string, error) {
29+
output, err := self.RunWithOutput(cmdStr)
30+
if err != nil {
31+
return "", err
32+
}
33+
output = strings.TrimRight(output, "\r\n")
34+
35+
if strings.Contains(output, "\r\n") {
36+
return "", fmt.Errorf("command output contains newlines: %s", output)
37+
}
38+
39+
return output, nil
40+
}

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,24 +333,12 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
333333
var err error
334334

335335
suggestedBranchName, err = utils.ResolveTemplate(self.c.UserConfig().Git.BranchPrefix, nil, template.FuncMap{
336-
"runCommand": func(command string) (string, error) {
337-
output, err := self.c.Git().Custom.RunWithOutput(command)
338-
if err != nil {
339-
return "", err
340-
}
341-
342-
output = strings.TrimRight(output, "\r\n")
343-
344-
if strings.ContainsAny(output, "\r\n\t ") {
345-
return "", fmt.Errorf("command output contains whitespace characters: %s", output)
346-
}
347-
348-
return output, nil
349-
},
336+
"runCommand": self.c.Git().Custom.TemplateFunctionRunCommand,
350337
})
351338
if err != nil {
352339
return err
353340
}
341+
suggestedBranchName = strings.ReplaceAll(suggestedBranchName, "\t", " ")
354342
}
355343

356344
refresh := func() error {

pkg/gui/services/custom_commands/handler_creator.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,8 @@ func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptR
246246
}
247247

248248
funcs := template.FuncMap{
249-
"quote": self.c.OS().Quote,
250-
"runCommand": func(command string) (string, error) {
251-
output, err := self.c.Git().Custom.RunWithOutput(command)
252-
if err != nil {
253-
return "", err
254-
}
255-
output = strings.TrimRight(output, "\r\n")
256-
257-
if strings.Contains(output, "\r\n") {
258-
return "", fmt.Errorf("command output contains newlines: %s", output)
259-
}
260-
261-
return output, nil
262-
},
249+
"quote": self.c.OS().Quote,
250+
"runCommand": self.c.Git().Custom.TemplateFunctionRunCommand,
263251
}
264252

265253
return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects, funcs) }

0 commit comments

Comments
 (0)