Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit dbb71bd

Browse files
committed
feat: support load hook configs from env
1 parent 3515a67 commit dbb71bd

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

internal/flags/flags.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flags
22

33
import (
44
"flag"
5+
"strings"
56

67
"github.com/adnanh/webhook/internal/hook"
78
"github.com/adnanh/webhook/internal/rules"
@@ -25,6 +26,15 @@ func ParseEnvs() AppFlags {
2526
flags.SetUID = GetEnvInt(ENV_KEY_UID, DEFAULT_UID)
2627
flags.HttpMethods = GetEnvStr(ENV_KEY_HTTP_METHODS, DEFAULT_HTTP_METHODS)
2728
flags.PidPath = GetEnvStr(ENV_KEY_PID_FILE, DEFAULT_PID_FILE)
29+
30+
hooks := strings.Split(GetEnvStr(ENV_KEY_HOOKS, ""), ",")
31+
var hooksFiles hook.HooksFiles
32+
for _, hook := range hooks {
33+
hooksFiles.Set(hook)
34+
}
35+
if len(hooksFiles) > 0 {
36+
flags.HooksFiles = hooksFiles
37+
}
2838
return flags
2939
}
3040

@@ -47,11 +57,12 @@ func ParseCLI(flags AppFlags) AppFlags {
4757
HttpMethods = flag.String("http-methods", DEFAULT_HTTP_METHODS, `set default allowed HTTP methods (ie. "POST"); separate methods with comma`)
4858
PidPath = flag.String("pidfile", DEFAULT_PID_FILE, "create PID file at the given path")
4959

50-
JustDisplayVersion = flag.Bool("version", false, "display webhook version and quit")
51-
ResponseHeaders hook.ResponseHeaders
60+
ShowVersion = flag.Bool("version", false, "display webhook version and quit")
61+
ResponseHeaders hook.ResponseHeaders
5262
)
5363

54-
flag.Var(&rules.HooksFiles, "hooks", "path to the json file containing defined hooks the webhook should serve, use multiple times to load from different files")
64+
hooksFiles := rules.HooksFiles
65+
flag.Var(&hooksFiles, "hooks", "path to the json file containing defined hooks the webhook should serve, use multiple times to load from different files")
5566
flag.Var(&ResponseHeaders, "header", "response header to return, specified in format name=value, use multiple times to set multiple headers")
5667

5768
flag.Parse()
@@ -120,13 +131,14 @@ func ParseCLI(flags AppFlags) AppFlags {
120131
flags.PidPath = *PidPath
121132
}
122133

123-
if *JustDisplayVersion {
134+
if *ShowVersion {
124135
flags.ShowVersion = true
125136
}
126137

127-
if len(rules.HooksFiles) > 0 {
128-
flags.HooksFiles = rules.HooksFiles
138+
if len(hooksFiles) > 0 {
139+
flags.HooksFiles = append(flags.HooksFiles, hooksFiles...)
129140
}
141+
rules.HooksFiles = flags.HooksFiles
130142

131143
if len(ResponseHeaders) > 0 {
132144
flags.ResponseHeaders = ResponseHeaders

0 commit comments

Comments
 (0)