forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-script-exec.go
58 lines (47 loc) · 1.25 KB
/
command-script-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
50
51
52
53
54
55
56
57
58
package toolkit
import (
"bytes"
"log"
"os"
"os/exec"
"strings"
)
// CommandExecutor .
type CommandExecutor interface {
// RunCommand .
RunCommand(execPath string, execArgs ...string) error
RunBashCommand(scriptPath string, execArgs ...string) error
}
//EmptyArgs .
const EmptyArgs string = ""
//KymaCommandExecutor .
type KymaCommandExecutor struct {
}
//RunCommand .
func (kymaBashExecutor *KymaCommandExecutor) RunCommand(execPath string, execArgs ...string) error {
cmd := exec.Command(execPath, execArgs...)
var stderr bytes.Buffer
var stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
errStr := string(stderr.Bytes())
outStr := string(stdout.Bytes())
log.Println("Bash script error")
log.Println(strings.Trim(errStr, "\n"))
log.Println("Bash script output")
log.Println(strings.Trim(outStr, "\n"))
return err
}
return nil
}
//RunBashCommand .
func (kymaBashExecutor *KymaCommandExecutor) RunBashCommand(scriptPath string, execArgs ...string) error {
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
log.Printf("%s doesn't exist.", scriptPath)
return nil
}
execArgs = append([]string{scriptPath}, execArgs...)
return kymaBashExecutor.RunCommand("/bin/bash", execArgs...)
}