diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-01 20:10:24 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-01 20:25:30 +1100 |
commit | e8e59306fcd20fb9ef73424f07893bce32edd442 (patch) | |
tree | 10d133f22d46ce604fadcc1ac4dc172ce5fe7e4e | |
parent | 8af3fe3b4ab46373440e5ec934964f05004f6727 (diff) |
shell out custom commands
-rw-r--r-- | pkg/commands/oscommands/os.go | 31 | ||||
-rw-r--r-- | pkg/gui/custom_commands.go | 6 | ||||
-rw-r--r-- | pkg/gui/files_panel.go | 9 |
3 files changed, 26 insertions, 20 deletions
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 <command>'. 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 }, }) |