forked from forj-oss/goforjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-cmd.go
38 lines (33 loc) · 931 Bytes
/
plugin-cmd.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
package goforjj
import (
"fmt"
"os/exec"
"strings"
"syscall"
"github.com/forj-oss/forjj-modules/trace"
)
// Generic start command.
// If return code is 0 return the command output
// If return code is 1 return empty string
// else return the error string in err
func cmd_run(cmd_args []string) (string, error) {
gotrace.Trace("RUNNING: '%s'", strings.Join(cmd_args, "' '"))
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
var ret []byte
ret, err := cmd.CombinedOutput()
if err == nil {
gotrace.Trace("RET: %s", string(ret))
return string(ret), nil
}
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
switch status.ExitStatus() {
case 0:
gotrace.Trace("RET: %s", string(ret))
return string(ret), nil
case 1:
gotrace.Trace("RET: %s", string(ret))
return "", nil
default:
return "", fmt.Errorf("ERROR: '%s' returns: %d. %s\n", cmd_args[0], status.ExitStatus(), cmd.ProcessState.String())
}
}