aboutsummaryrefslogtreecommitdiff
path: root/hook.go
blob: a5c84dafea12f9418a2bd22815e19fd296ed502a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package acme

import (
	"log"
	"os/exec"
	"strings"
)

type Hooker map[string]bool

func execHook(s string) error {
	cmd := strings.Fields(s)
	return exec.Command(cmd[0], cmd[1:]...).Run()
}

/*
func NewHooker() chan string {
	h := make(chan string, 2)
	go func() {
		for hook := range h {
			if err := execHook(hook); err != nil {
				log.Println(hook, err)
			}
		}
	}()
	return h
}
*/

func NewHooker() Hooker {
	return make(Hooker)
}

func (h Hooker) Notice(cmd string) {
	h[cmd] = true
}

func (h Hooker) Execute() {
	for cmd := range h {
		if err := execHook(cmd); err != nil {
			log.Println(cmd, err)
		}
	}
}