-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
47 lines (42 loc) · 1.09 KB
/
commands.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
package util
import (
"fmt"
"os"
"os/exec"
"strings"
)
func PathWithBinary() string {
path := os.Getenv("PATH")
binDir, _ := BinaryLocation()
return path + string(os.PathListSeparator) + binDir
}
// GetCommandOutput evaluates the given command and returns the trimmed output
func GetCommandOutput(dir string, name string, args ...string) (string, error) {
os.Setenv("PATH", PathWithBinary())
e := exec.Command(name, args...)
if dir != "" {
e.Dir = dir
}
data, err := e.CombinedOutput()
text := string(data)
text = strings.TrimSpace(text)
if err != nil {
return text, fmt.Errorf("Error: Command failed %s %s %s %s\n", name, strings.Join(args, " "), text, err)
}
return text, err
}
// RunCommand evaluates the given command and returns the trimmed output
func RunCommand(dir string, name string, args ...string) error {
os.Setenv("PATH", PathWithBinary())
e := exec.Command(name, args...)
if dir != "" {
e.Dir = dir
}
e.Stdout = os.Stdout
e.Stderr = os.Stdin
err := e.Run()
if err != nil {
fmt.Printf("Error: Command failed %s %s\n", name, strings.Join(args, " "))
}
return err
}