From b691c0affc0c82faea9eca47380d19ba7d133ac3 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 13 Dec 2020 02:08:44 -0500 Subject: [PATCH] feat: properly handle stdout when capturing plugn output The previous output would also include a message like `exit status 1`. This allows us to silence that output and instead only capture that which was return from the subprocess. --- plugins/common/subprocess.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/common/subprocess.go b/plugins/common/subprocess.go index 179dc0420..8257a8e51 100644 --- a/plugins/common/subprocess.go +++ b/plugins/common/subprocess.go @@ -2,6 +2,7 @@ package common import ( "fmt" + "io/ioutil" "os" "os/exec" "strings" @@ -79,7 +80,21 @@ func PlugnTrigger(triggerName string, args ...string) error { // PlugnTriggerOutput fire the given plugn trigger with the given args func PlugnTriggerOutput(triggerName string, args ...string) ([]byte, error) { - return PlugnTriggerSetup(triggerName, args...).Output() + rescueStderr := os.Stderr + r, w, _ := os.Pipe() + os.Stderr = w + out, err := PlugnTriggerSetup(triggerName, args...).Output() + w.Close() + + readStderr, _ := ioutil.ReadAll(r) + os.Stderr = rescueStderr + + var stderr error + if err != nil { + stderr = fmt.Errorf(string(readStderr[:])) + } + + return out, stderr } // PlugnTriggerSetup sets up a plugn trigger call