1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-15 11:56:37 +02:00

shell out custom commands

This commit is contained in:
Jesse Duffield 2021-04-01 20:10:24 +11:00
parent 8af3fe3b4a
commit e8e59306fc
3 changed files with 26 additions and 20 deletions

View File

@ -172,6 +172,17 @@ func (c *OSCommand) RunCommand(formatString string, formatArgs ...interface{}) e
return err 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 // FileType tells us if the file is a file, directory or other
func (c *OSCommand) FileType(path string) string { func (c *OSCommand) FileType(path string) string {
fileInfo, err := os.Stat(path) fileInfo, err := os.Stat(path)
@ -184,16 +195,6 @@ func (c *OSCommand) FileType(path string) string {
return "file" 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) { func sanitisedCommandOutput(output []byte, err error) (string, error) {
outputString := string(output) outputString := string(output)
if err != nil { if err != nil {
@ -241,6 +242,11 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex
return cmd 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 // Quote wraps a message in platform-specific quotation marks
func (c *OSCommand) Quote(message string) string { func (c *OSCommand) Quote(message string) string {
if c.Platform.OS == "windows" { if c.Platform.OS == "windows" {
@ -349,11 +355,6 @@ func (c *OSCommand) GetLazygitPath() string {
return `"` + filepath.ToSlash(ex) + `"` 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 // PipeCommands runs a heap of commands and pipes their inputs/outputs together like A | B | C
func (c *OSCommand) PipeCommands(commandStrings ...string) error { func (c *OSCommand) PipeCommands(commandStrings ...string) error {

View File

@ -58,7 +58,7 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand
} }
if customCommand.Subprocess { if customCommand.Subprocess {
gui.PrepareSubProcess(cmdStr) gui.PrepareShellSubProcess(cmdStr)
return nil return nil
} }
@ -67,9 +67,7 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand
loadingText = gui.Tr.LcRunningCustomCommandStatus loadingText = gui.Tr.LcRunningCustomCommandStatus
} }
return gui.WithWaitingStatus(loadingText, func() error { return gui.WithWaitingStatus(loadingText, func() error {
gui.OSCommand.PrepareSubProcess(cmdStr) if err := gui.OSCommand.RunShellCommand(cmdStr); err != nil {
if err := gui.OSCommand.RunCommand(cmdStr); err != nil {
return gui.surfaceError(err) return gui.surfaceError(err)
} }
return gui.refreshSidePanels(refreshOptions{}) return gui.refreshSidePanels(refreshOptions{})

View File

@ -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 { func (gui *Gui) editFile(filename string) error {
_, err := gui.runSyncOrAsyncCommand(gui.GitCommand.EditFile(filename)) _, err := gui.runSyncOrAsyncCommand(gui.GitCommand.EditFile(filename))
return err return err
@ -798,7 +805,7 @@ func (gui *Gui) handleCustomCommand(g *gocui.Gui, v *gocui.View) error {
return gui.prompt(promptOpts{ return gui.prompt(promptOpts{
title: gui.Tr.CustomCommand, title: gui.Tr.CustomCommand,
handleConfirm: func(command string) error { handleConfirm: func(command string) error {
gui.SubProcess = gui.OSCommand.RunCustomCommand(command) gui.SubProcess = gui.OSCommand.PrepareShellSubProcess(command)
return gui.Errors.ErrSubProcess return gui.Errors.ErrSubProcess
}, },
}) })