-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (75 loc) · 1.68 KB
/
main.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
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"strconv"
"github.com/phayes/hookserve/hookserve"
)
var (
pushPort int
pushNotifHTTPPath string
pushGithubSecret string
projectDir string
)
func main() {
var err error
if pushPort, err = strconv.Atoi(os.Getenv("PUSH_PORT")); err != nil {
log.Fatal("Env PUSH_PORT must be integer")
}
pushNotifHTTPPath = os.Getenv("PUSH_HTTP_PATH")
if pushNotifHTTPPath == "" {
log.Fatal("Env PUSH_HTTP_PATH must be present")
}
pushGithubSecret = os.Getenv("PUSH_GITHUB_SECRET")
projectDir = os.Getenv("PUSH_PROJECT_DIR")
if projectDir == "" {
log.Fatal("Env PUSH_PROJECT_DIR must be present")
}
server := hookserve.NewServer()
server.Port = pushPort
server.Path = pushNotifHTTPPath
server.Secret = pushGithubSecret
server.GoListenAndServe()
cliExecuter := cliRunner{os.Stderr}
fmt.Printf("Starting server...\nExpecting push notification on :%d%s\nProject path %s\n", pushPort, pushNotifHTTPPath, projectDir)
for {
select {
case event := <-server.Events:
err := pull(event, &cliExecuter)
if err != nil {
log.Fatal(err)
}
err = build(&cliExecuter)
if err != nil {
log.Fatal(err)
}
}
}
}
type runner interface {
Run(cmd *exec.Cmd) error
}
type cliRunner struct {
log io.Writer
}
func (cli *cliRunner) Run(cmd *exec.Cmd) error {
cmd.Stderr = cli.log
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func pull(event hookserve.Event, executer runner) error {
cliCmd := exec.Command("git", "pull")
cliCmd.Dir = projectDir
return executer.Run(cliCmd)
}
func build(executer runner) error {
cmd := exec.Command("hugo")
cmd.Dir = projectDir
return executer.Run(cmd)
}