I am trying to fire a command via exec in my go binary to get JSON output of the other script. The other script is a nodejs task checking for html problems. If I fire the nodeJS task on cli everything works fine and I get JSON output, but if I fire the command inside of go I only get:
exit status 1
I am not total sure if the problem is a nodejs or a go problem, but even if nodejs founds HTML Problems I want to be able to analyze the JSON Response in my go script.
Here is my source code:
out, err := exec.Command("/usr/bin/testcafe", "'chromium:headless --no-sandbox'", "--reporter json", "/data/checks.js").Output()
status := http.StatusOK
message := ""
if err != nil {
status = http.StatusNotImplemented
message = err.Error() + string(string(out[:]))
fmt.Println(message)
}
As mentioned above if you ever need to access Stderr from an Command Exit in Golang use:
message += string(err.(*exec.ExitError).Stderr[:])
In my case the nodejs tool gave an exit code based on the amounts of problems. Solved this it runs perfectly now =).
I made a function that I use to do shell command execution: https://gist.github.com/leninhasda/8f2b5cdc22677a8f2bdf2e896eb0b561
stdOut, stdErr, err := ShellExec("echo", "hello", "world")
if err != nil {
// error executing command
}
if stdErr != "" {
// shell command returned error message
}
if stdOut != "" {
// shell command returned output message
// hello world
}