aboutsummaryrefslogblamecommitdiffstats
path: root/lib/hooks/exec.go
blob: 71542c99bd3d74f5fbdc21e87391aa0eced73f15 (plain) (tree)
1
2
3
4
5
6
7
8


             
               


                 
                                        










                                                                  


                                   

                                           





                                                                     
 
package hooks

import (
	"bytes"
	"os"
	"os/exec"

	"git.sr.ht/~rjarry/aerc/lib/log"
)

func RunHook(h HookType) error {
	cmd := h.Cmd()
	if cmd == "" {
		return nil
	}
	env := h.Env()
	log.Debugf("hooks: running command %q (env %v)", cmd, env)

	proc := exec.Command("sh", "-c", cmd)
	var outb, errb bytes.Buffer
	proc.Stdout = &outb
	proc.Stderr = &errb
	proc.Env = os.Environ()
	proc.Env = append(proc.Env, env...)
	err := proc.Run()
	log.Tracef("hooks: %q stdout: %s", cmd, outb.String())
	if err != nil {
		log.Errorf("hooks:%q stderr: %s", cmd, errb.String())
	}
	return err
}