You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
noPanic=flag.Bool("nopanic", false, "do not panic if hooks cannot be loaded when webhook is not running in verbose mode")
32
32
hotReload=flag.Bool("hotreload", false, "watch hooks file for changes and reload them automatically")
33
-
hooksFilePath=flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve")
34
33
hooksURLPrefix=flag.String("urlprefix", "hooks", "url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id)")
35
34
secure=flag.Bool("secure", false, "use HTTPS instead of HTTP")
36
35
cert=flag.String("cert", "cert.pem", "path to the HTTPS certificate pem file")
37
36
key=flag.String("key", "key.pem", "path to the HTTPS certificate private key pem file")
38
37
justDisplayVersion=flag.Bool("version", false, "display webhook version and quit")
39
38
40
39
responseHeaders hook.ResponseHeaders
40
+
hooksFiles hook.HooksFiles
41
+
42
+
loadedHooksFromFiles=make(map[string]hook.Hooks)
41
43
42
44
watcher*fsnotify.Watcher
43
45
signalschan os.Signal
44
-
45
-
hooks hook.Hooks
46
46
)
47
47
48
-
funcmain() {
49
-
hooks= hook.Hooks{}
48
+
funcmatchLoadedHook(idstring) *hook.Hook {
49
+
for_, hooks:=rangeloadedHooksFromFiles {
50
+
ifhook:=hooks.Match(id); hook!=nil {
51
+
returnhook
52
+
}
53
+
}
54
+
55
+
returnnil
56
+
}
57
+
58
+
funclenLoadedHooks() int {
59
+
sum:=0
60
+
for_, hooks:=rangeloadedHooksFromFiles {
61
+
sum+=len(hooks)
62
+
}
63
+
64
+
returnsum
65
+
}
50
66
67
+
funcmain() {
68
+
flag.Var(&hooksFiles, "hooks", "path to the json file containing defined hooks the webhook should serve, use multiple times to load from different files")
51
69
flag.Var(&responseHeaders, "header", "response header to return, specified in format name=value, use multiple times to set multiple headers")
52
70
53
71
flag.Parse()
@@ -57,6 +75,10 @@ func main() {
57
75
os.Exit(0)
58
76
}
59
77
78
+
iflen(hooksFiles) ==0 {
79
+
hooksFiles=append(hooksFiles, "hooks.json")
80
+
}
81
+
60
82
log.SetPrefix("[webhook] ")
61
83
log.SetFlags(log.Ldate|log.Ltime)
62
84
@@ -70,50 +92,63 @@ func main() {
70
92
setupSignals()
71
93
72
94
// load and parse hooks
73
-
log.Printf("attempting to load hooks from %s\n", *hooksFilePath)
95
+
for_, hooksFilePath:=rangehooksFiles {
96
+
log.Printf("attempting to load hooks from %s\n", hooksFilePath)
74
97
75
-
err:=hooks.LoadFromFile(*hooksFilePath)
76
-
77
-
iferr!=nil {
78
-
if!*verbose&&!*noPanic {
79
-
log.SetOutput(os.Stdout)
80
-
log.Fatalf("couldn't load any hooks from file! %+v\naborting webhook execution since the -verbose flag is set to false.\nIf, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic", err)
81
-
}
98
+
newHooks:= hook.Hooks{}
82
99
83
-
log.Printf("couldn't load hooks from file! %+v\n", err)
84
-
} else {
85
-
seenHooksIds:=make(map[string]bool)
100
+
err:=newHooks.LoadFromFile(hooksFilePath)
86
101
87
-
log.Printf("found %d hook(s) in file\n", len(hooks))
102
+
iferr!=nil {
103
+
log.Printf("couldn't load hooks from file! %+v\n", err)
104
+
} else {
105
+
log.Printf("found %d hook(s) in file\n", len(newHooks))
88
106
89
-
for_, hook:=rangehooks {
90
-
ifseenHooksIds[hook.ID] ==true {
91
-
log.Fatalf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!\n", hook.ID)
107
+
for_, hook:=rangenewHooks {
108
+
ifmatchLoadedHook(hook.ID) !=nil {
109
+
log.Fatalf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!\n", hook.ID)
110
+
}
111
+
log.Printf("\tloaded: %s\n", hook.ID)
92
112
}
93
-
seenHooksIds[hook.ID] =true
94
-
log.Printf("\tloaded: %s\n", hook.ID)
113
+
114
+
loadedHooksFromFiles[hooksFilePath] =newHooks
95
115
}
96
116
}
97
117
98
-
if*hotReload {
99
-
// set up file watcher
100
-
log.Printf("setting up file watcher for %s\n", *hooksFilePath)
log.Fatalln("couldn't load any hooks from file!\naborting webhook execution since the -verbose flag is set to false.\nIf, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic")
log.Printf("removed %d hook(s) that were loaded from file %s\n", removedHooksCount, hooksFilePath)
404
+
405
+
if!*verbose&&!*noPanic&&lenLoadedHooks() ==0 {
406
+
log.SetOutput(os.Stdout)
407
+
log.Fatalln("couldn't load any hooks from file!\naborting webhook execution since the -verbose flag is set to false.\nIf, for some reason, you want webhook to run without the hooks, either use -verbose flag, or -nopanic")
0 commit comments