-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (50 loc) · 1.01 KB
/
main.go
File metadata and controls
57 lines (50 loc) · 1.01 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
)
type HookConfig struct {
PathRepo string `json:"path_repo"`
Port string `json:"port"`
HookCmd string `json:"hook_cmd"`
}
// 绝对路径
var pathRepo string
var port string = "5555"
func main() {
parseConfig()
http.HandleFunc("/payload", handlePayload)
fmt.Println("Listening on http://localhost:" + port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
}
}
func parseConfig() {
file, err := os.Open("hook_config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
conf := HookConfig{}
error := decoder.Decode(&conf)
if error != nil {
log.Fatal(error)
}
pathRepo = conf.PathRepo
port = conf.Port
}
func handlePayload(writer http.ResponseWriter, request *http.Request) {
var event = request.Header.Get("X-Github-Event")
switch event {
case "push":
exec.Command("cd", pathRepo)
exec.Command("git", "pull")
fmt.Println("git pull done!")
}
}