Skip to content

Commit

Permalink
use custom io.Writer for command errors for better and less logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dakyskye committed Jul 13, 2020
1 parent 9e95d35 commit 9c80a71
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions listener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"errors"
"os"
"os/exec"
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 9c80a71

Please sign in to comment.