From 3f0ad1c49230d83224d2c73474f7bbf4d90b53c8 Mon Sep 17 00:00:00 2001 From: Kir Melnikov Date: Thu, 19 Jan 2017 14:16:36 +0500 Subject: [PATCH] Bugfix/script (#27) * Fixed bug with script reading from stdin --- script/script.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/script/script.go b/script/script.go index 117c8cd..36e909d 100644 --- a/script/script.go +++ b/script/script.go @@ -1,9 +1,9 @@ package script import ( + "bytes" "encoding/json" "fmt" - "io" "os" "os/exec" "strings" @@ -73,13 +73,15 @@ func (sender *Sender) SendEvents(events notifier.EventsData, contact notifier.Co } c := exec.Command(scriptFile, args[1:]...) - stdin, _ := c.StdinPipe() - io.WriteString(stdin, string(scriptJSON)) - io.WriteString(stdin, "\n") - stdin.Close() - scriptOutput, err := c.CombinedOutput() + var scriptOutput bytes.Buffer + c.Stdin = strings.NewReader(string(scriptJSON)) + c.Stdout = &scriptOutput + log.Debugf("Executing script: %s", scriptFile) + err = c.Run() + log.Debugf("Finished executing: %s", scriptFile) + if err != nil { - return fmt.Errorf("Failed exec [%s] Error [%s] Output: [%s]", sender.Exec, err.Error(), string(scriptOutput)) + return fmt.Errorf("Failed exec [%s] Error [%s] Output: [%s]", sender.Exec, err.Error(), scriptOutput.String()) } return nil }