-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexec.go
32 lines (27 loc) · 853 Bytes
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package coreutils
import (
"os"
"os/exec"
)
// ExecCommand executes a command with args and returning the stringified output
func ExecCommand(command string, args []string, redirect bool) string {
if ExecutableExists(command) { // If the executable exists
var output []byte
runner := exec.Command(command, args...)
if redirect { // If we should redirect output to var
output, _ = runner.CombinedOutput() // Combine the output of stderr and stdout
} else {
runner.Stdout = os.Stdout
runner.Stderr = os.Stderr
runner.Run()
}
return string(output[:])
} else { // If the executable doesn't exist
return command + " is not an executable."
}
}
// ExecutableExists checks if an executable exists
func ExecutableExists(executableName string) bool {
_, existsErr := exec.LookPath(executableName)
return (existsErr == nil)
}