-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
49 lines (42 loc) · 1.05 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bytes"
"fmt"
"os/exec"
)
func runCommand(commandString string) {
defaultPath := []string{"/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/go/bin"}
commandFound := false
slicedCommandWithArgs := stringSlicer(commandString)
command := slicedCommandWithArgs[0]
args := slicedCommandWithArgs[1:]
if _, ok := builtins[command]; ok {
runBuiltin(command, args...)
return
}
for _, prefix := range defaultPath {
fullCommandPath := prefix + "/" + slicedCommandWithArgs[0]
if fileExists(fullCommandPath) {
commandFound = true
break
}
}
if commandFound {
stdout, stderr := execCommandString(command, args...)
fmt.Printf(stdout)
fmt.Printf(stderr)
} else {
Logger.Error().Msg("Couldn't find file")
}
}
func execCommandString(cmd string, args ...string) (string, string) {
cmder := exec.Command(cmd, args...)
var out, sterr bytes.Buffer
cmder.Stdout = &out
cmder.Stderr = &sterr
err := cmder.Run()
if err != nil {
Logger.Error().Err(err).Msg("")
}
return out.String(), sterr.String()
}