diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 809f9deab..8eaff53d2 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -172,6 +172,17 @@ func (c *OSCommand) RunCommand(formatString string, formatArgs ...interface{}) e return err } +// RunShellCommand runs shell commands i.e. 'sh -c '. Good for when you +// need access to the shell +func (c *OSCommand) RunShellCommand(command string) error { + c.Log.WithField("command", command).Info("RunShellCommand") + + cmd := c.Command(c.Platform.Shell, c.Platform.ShellArg, command) + _, err := sanitisedCommandOutput(cmd.CombinedOutput()) + + return err +} + // FileType tells us if the file is a file, directory or other func (c *OSCommand) FileType(path string) string { fileInfo, err := os.Stat(path) @@ -184,16 +195,6 @@ func (c *OSCommand) FileType(path string) string { return "file" } -// RunDirectCommand wrapper around direct commands -func (c *OSCommand) RunDirectCommand(command string) (string, error) { - c.Log.WithField("command", command).Info("RunDirectCommand") - - return sanitisedCommandOutput( - c.Command(c.Platform.Shell, c.Platform.ShellArg, command). - CombinedOutput(), - ) -} - func sanitisedCommandOutput(output []byte, err error) (string, error) { outputString := string(output) if err != nil { @@ -241,6 +242,11 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex return cmd } +// PrepareShellSubProcess returns the pointer to a custom command +func (c *OSCommand) PrepareShellSubProcess(command string) *exec.Cmd { + return c.PrepareSubProcess(c.Platform.Shell, c.Platform.ShellArg, command) +} + // Quote wraps a message in platform-specific quotation marks func (c *OSCommand) Quote(message string) string { if c.Platform.OS == "windows" { @@ -349,11 +355,6 @@ func (c *OSCommand) GetLazygitPath() string { return `"` + filepath.ToSlash(ex) + `"` } -// RunCustomCommand returns the pointer to a custom command -func (c *OSCommand) RunCustomCommand(command string) *exec.Cmd { - return c.PrepareSubProcess(c.Platform.Shell, c.Platform.ShellArg, command) -} - // PipeCommands runs a heap of commands and pipes their inputs/outputs together like A | B | C func (c *OSCommand) PipeCommands(commandStrings ...string) error { diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index f1fadeb5f..670d78df6 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -58,7 +58,7 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand } if customCommand.Subprocess { - gui.PrepareSubProcess(cmdStr) + gui.PrepareShellSubProcess(cmdStr) return nil } @@ -67,9 +67,7 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand loadingText = gui.Tr.LcRunningCustomCommandStatus } return gui.WithWaitingStatus(loadingText, func() error { - gui.OSCommand.PrepareSubProcess(cmdStr) - - if err := gui.OSCommand.RunCommand(cmdStr); err != nil { + if err := gui.OSCommand.RunShellCommand(cmdStr); err != nil { return gui.surfaceError(err) } return gui.refreshSidePanels(refreshOptions{}) diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index d14e1f6c8..b1cb66f82 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -492,6 +492,13 @@ func (gui *Gui) PrepareSubProcess(command string) { }) } +func (gui *Gui) PrepareShellSubProcess(command string) { + gui.SubProcess = gui.OSCommand.PrepareShellSubProcess(command) + gui.g.Update(func(g *gocui.Gui) error { + return gui.Errors.ErrSubProcess + }) +} + func (gui *Gui) editFile(filename string) error { _, err := gui.runSyncOrAsyncCommand(gui.GitCommand.EditFile(filename)) return err @@ -798,7 +805,7 @@ func (gui *Gui) handleCustomCommand(g *gocui.Gui, v *gocui.View) error { return gui.prompt(promptOpts{ title: gui.Tr.CustomCommand, handleConfirm: func(command string) error { - gui.SubProcess = gui.OSCommand.RunCustomCommand(command) + gui.SubProcess = gui.OSCommand.PrepareShellSubProcess(command) return gui.Errors.ErrSubProcess }, })