-
Notifications
You must be signed in to change notification settings - Fork 26
/
hooks.go
163 lines (151 loc) · 3.96 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"html/template"
"os/exec"
"time"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/plugintypes"
)
func (a *goBlog) preStartHooks() {
cfg := a.cfg.Hooks
for _, cmd := range cfg.PreStart {
func(cmd string) {
a.executeHookCommand("pre-start", cfg.Shell, cmd)
}(cmd)
}
}
type postHookFunc func(*post)
func (a *goBlog) postPostHooks(p *post) {
// Hooks after post published
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostPost {
go func(p *post, cmdTmplString string) {
a.executeHookTemplateCommand("post-post", cmdTmplString, map[string]any{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pPostHooks {
go f(p)
}
for _, plugin := range a.getPlugins(pluginPostCreatedHookType) {
go plugin.(plugintypes.PostCreatedHook).PostCreated(p)
}
}
func (a *goBlog) postUpdateHooks(p *post) {
// Hooks after post updated
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostUpdate {
go func(p *post, cmdTmplString string) {
a.executeHookTemplateCommand("post-update", cmdTmplString, map[string]any{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pUpdateHooks {
go f(p)
}
for _, plugin := range a.getPlugins(pluginPostUpdatedHookType) {
go plugin.(plugintypes.PostUpdatedHook).PostUpdated(p)
}
}
func (a *goBlog) postDeleteHooks(p *post) {
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostDelete {
go func(p *post, cmdTmplString string) {
a.executeHookTemplateCommand("post-delete", cmdTmplString, map[string]any{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pDeleteHooks {
go f(p)
}
for _, plugin := range a.getPlugins(pluginPostDeletedHookType) {
go plugin.(plugintypes.PostDeletedHook).PostDeleted(p)
}
}
func (a *goBlog) postUndeleteHooks(p *post) {
if hc := a.cfg.Hooks; hc != nil {
for _, cmdTmplString := range hc.PostUndelete {
go func(p *post, cmdTmplString string) {
a.executeHookTemplateCommand("post-undelete", cmdTmplString, map[string]any{
"URL": a.fullPostURL(p),
"Post": p,
})
}(p, cmdTmplString)
}
}
for _, f := range a.pUndeleteHooks {
go f(p)
}
}
func (a *goBlog) executeHookTemplateCommand(hookType string, tmpl string, data map[string]any) {
cfg := a.cfg.Hooks
cmdTmpl, err := template.New("cmd").Parse(tmpl)
if err != nil {
a.error("Failed to parse cmd template", "err", err)
return
}
cmdBuf := bufferpool.Get()
defer bufferpool.Put(cmdBuf)
if err = cmdTmpl.Execute(cmdBuf, data); err != nil {
a.error("Failed to execute cmd template", "err", err)
return
}
a.executeHookCommand(hookType, cfg.Shell, cmdBuf.String())
}
type hourlyHookFunc func()
func (a *goBlog) startHourlyHooks() {
cfg := a.cfg.Hooks
// Add configured hourly hooks
for _, cmd := range cfg.Hourly {
c := cmd
f := func() {
a.executeHookCommand("hourly", cfg.Shell, c)
}
a.hourlyHooks = append(a.hourlyHooks, f)
}
// When there are hooks, start ticker
if len(a.hourlyHooks) > 0 {
// Wait for next full hour
tr := time.AfterFunc(time.Until(time.Now().Truncate(time.Hour).Add(time.Hour)), func() {
// Execute once
for _, f := range a.hourlyHooks {
go f()
}
// Start ticker and execute regularly
ticker := time.NewTicker(1 * time.Hour)
a.shutdown.Add(func() {
ticker.Stop()
a.info("Stopped hourly hooks")
})
for range ticker.C {
for _, f := range a.hourlyHooks {
go f()
}
}
})
a.shutdown.Add(func() {
if tr.Stop() {
a.info("Canceled hourly hooks")
}
})
}
}
func (a *goBlog) executeHookCommand(hookType, shell, cmd string) {
a.info("Executing hook", "type", hookType, "cmd", cmd)
out, err := exec.Command(shell, "-c", cmd).CombinedOutput()
if err != nil {
a.error("Failed to execute command", "err", err, "cmd", cmd)
}
if len(out) > 0 {
a.info("Hook output", "out", string(out))
}
}