/
hooks.go
65 lines (55 loc) · 1.23 KB
/
hooks.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package agent
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)
var ErrHookNotFound error = errors.New("unknown hook")
// hooksCtx is used to define how to execute the hook scripts.
// When creating, the environment variables are provided.
type Hooks struct {
Path string
env []string
}
func (h *Hooks) WithEnv(env []string) *Hooks {
return &Hooks{
Path: h.Path,
env: env,
}
}
func (h Hooks) _fire(name string) error {
_, err := os.Stat(filepath.Join(h.Path, name))
if err != nil {
if os.IsNotExist(err) {
return ErrHookNotFound
}
return err
}
log.Printf("Firing hook %s\n", name)
cmd := exec.Command(filepath.Join(h.Path, name))
cmd.Env = h.env
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("hook error: %w", err)
}
return nil
}
func (h Hooks) fireCustom(name string) error {
if name == "post_success" || name == "post_failure" || name == "pre_task" {
return fmt.Errorf("reserved hook name: %s", name)
}
return h._fire(name)
}
func (h Hooks) firePostSuccess() error {
return h._fire("post_success")
}
func (h Hooks) firePostFailure() error {
return h._fire("post_failure")
}
func (h Hooks) firePreTask() error {
return h._fire("pre_task")
}