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 /pkg/commands/oscommands | |
parent | 8af3fe3b4ab46373440e5ec934964f05004f6727 (diff) |
shell out custom commands
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r-- | pkg/commands/oscommands/os.go | 31 |
1 files changed, 16 insertions, 15 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 { |