From 9c80a71eaa3734852f022f8469fbd71e765a25d0 Mon Sep 17 00:00:00 2001 From: dakyskye Date: Mon, 13 Jul 2020 21:59:25 +0400 Subject: [PATCH] use custom io.Writer for command errors for better and less logging --- listener.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/listener.go b/listener.go index 95cd5a4..f27431b 100644 --- a/listener.go +++ b/listener.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "errors" "os" "os/exec" @@ -54,14 +55,23 @@ func listenKeybinding(X *xgbutil.XUtil, errs chan<- error, evtType int, shell, k // do a given shell command func doAction(err chan<- error, shell, do string) { + writer := new(bytes.Buffer) cmd := exec.Command(shell) cmd.Stdin = strings.NewReader(do) cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stderr = writer cmd.SysProcAttr = &syscall.SysProcAttr{ Foreground: false, Setsid: true, } zap.L().Debug("now executing a command", zap.String("command", do)) - err <- cmd.Run() + err <- cmd.Start() + if e := cmd.Wait(); e != nil { + prefixLen := len(shell) + 2 + if writer.Len() > prefixLen { + err <- errors.New(writer.String()[prefixLen:]) + } else { + err <- e + } + } }