Skip to content

Commit

Permalink
feat(pkg.tasker): capture cmd output in tasker logger, error in stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed May 5, 2021
1 parent eb928c4 commit 0da0aae
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/tasker/tasker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,27 @@ func New(opt Option) *Tasker {
}

// Taskify creates TaskFunc out of plain command wrt given options.
func Taskify(cmd string, opt Option) TaskFunc {
func (t *Tasker) Taskify(cmd string, opt Option) TaskFunc {
sh := Shell(opt.Shell)

return func(ctx context.Context) (int, error) {
err := exec.Command(sh[0], sh[1], cmd).Run()
buf := strings.Builder{}
exc := exec.Command(sh[0], sh[1], cmd)
exc.Stderr = &buf

if t.Log.Writer() != exc.Stderr {
exc.Stdout = t.Log.Writer()
}

err := exc.Run()
if err == nil {
return 0, nil
}

for _, ln := range strings.Split(strings.TrimRight(buf.String(), "\r\n"), "\n") {
log.Println(ln)
}

code := 1
if exErr, ok := err.(*exec.ExitError); ok {
code = exErr.ExitCode()
Expand Down

0 comments on commit 0da0aae

Please sign in to comment.