2019-09-21 22:03:26 +00:00
|
|
|
package main
|
|
|
|
|
2019-10-01 15:26:07 +00:00
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
2019-09-21 22:03:26 +00:00
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
// escapeCommandShell accepts a command to be executed by a shell and escapes it
|
2019-10-01 15:26:07 +00:00
|
|
|
func escapeCommandShell(command string) string {
|
|
|
|
// Remove extra spaces and newlines from ends
|
|
|
|
command = strings.TrimSpace(command)
|
|
|
|
// TODO: Not sure if this part is actually needed. Should verify
|
|
|
|
// Escape double quotes since this will be passed in as an argument
|
|
|
|
command = strings.Replace(command, `"`, `\"`, -1)
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2019-10-02 23:09:11 +00:00
|
|
|
// ShellCommand takes a string and executes it as a command using `sh`
|
2019-10-01 15:26:07 +00:00
|
|
|
func ShellCommand(command string) *exec.Cmd {
|
|
|
|
shellCommand := []string{"sh", "-c", escapeCommandShell(command)}
|
2019-10-02 16:37:29 +00:00
|
|
|
//log.Printf("Shell command: %v", shellCommand)
|
2019-10-01 15:26:07 +00:00
|
|
|
return exec.Command(shellCommand[0], shellCommand[1:]...)
|
|
|
|
}
|