Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed May 9, 2018
1 parent a8508cd commit c4e99e6
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,46 @@ func (h *HookExec) Exec(timeout time.Duration) error {
defer h.HookServer.Unlock()

for _, f := range files {
cmd := exec.Command(f)
err := execFile(f, h, timeout)
multierror.Append(result, err)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return result
}

stdin, err := cmd.StdinPipe()
if err != nil {
multierror.Append(result, err)
continue
}
defer stdin.Close()
func execFile(f string, h *HookExec, timeout time.Duration) error {
cmd := exec.Command(f)

// io.Copy( cmd.StdinPipe
err = cmd.Start()
if err != nil {
multierror.Append(result, err)
continue
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
defer stdin.Close()

h.Data.Seek(0, 0)
io.Copy(stdin, h.Data)
stdin.Close()
err = cmd.Start()
if err != nil {
return err
}

timer := time.AfterFunc(timeout, func() {
cmd.Process.Kill()
})
h.Data.Seek(0, 0)
io.Copy(stdin, h.Data)
stdin.Close()

err = cmd.Wait()
timer.Stop()
timer := time.AfterFunc(timeout, func() {
cmd.Process.Kill()
})

if err != nil {
multierror.Append(result, err)
continue
}
err = cmd.Wait()
timer.Stop()

if err != nil {
return err
}

return result
return nil
}

// todo: base this on OS
Expand Down

0 comments on commit c4e99e6

Please sign in to comment.