-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.go
More file actions
63 lines (50 loc) · 1.06 KB
/
exec.go
File metadata and controls
63 lines (50 loc) · 1.06 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package helpers
import (
"os/exec"
"runtime"
"strings"
)
// Get the underlying OS command shell
func getOSC() string {
osc := "sh"
if runtime.GOOS == "windows" {
osc = "cmd"
}
return osc
}
// Get the shell/command startup option to execute commands
func getOSE() string {
ose := "-c"
if runtime.GOOS == "windows" {
ose = "/c"
}
return ose
}
// ExecutableExists -
func ExecutableExists(command string) bool {
_, err := exec.LookPath(command)
if err != nil {
return false
}
return true
}
// Exec -
func Exec(command string) (string, error) {
return ExecInFolder(command, "")
}
// ExecInFolder -
func ExecInFolder(command string, folder string) (string, error) {
osc := getOSC()
ose := getOSE()
cmd := exec.Command(osc, ose, command)
if len(strings.TrimSpace(folder)) > 0 {
cmd.Dir = folder
}
output, err := cmd.CombinedOutput()
return string(output), err
}
// ExecWithArgs -
func ExecWithArgs(name string, args ...string) (out string, err error) {
output, err := exec.Command(name, args...).CombinedOutput()
return string(output), err
}