-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.go
109 lines (102 loc) · 2.15 KB
/
setup.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
package dreck
import (
"fmt"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
func setup(c *caddy.Controller) error {
dr, err := parseConfig(c)
if err != nil {
return err
}
if dr.clientID == "" {
return fmt.Errorf("need a valid client_id")
}
if dr.key == "" {
return fmt.Errorf("need a path to a private key file")
}
if dr.secret == "" {
return fmt.Errorf("need a webhook secret")
}
if !dr.hmac {
return fmt.Errorf("need a to validate the webhook secret")
}
dreck := func(next httpserver.Handler) httpserver.Handler {
dr.Next = next
return dr
}
httpserver.GetConfig(c).AddMiddleware(dreck)
return nil
}
func parseConfig(c *caddy.Controller) (Dreck, error) {
d := New()
for c.Next() {
for c.NextBlock() {
switch c.Val() {
case "client_id":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
d.clientID = args[0]
case "private_key":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
d.key = args[0]
case "owners":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
d.owners = args[0]
case "secret":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
d.secret = args[0]
case "path":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
d.path = args[0]
case "validate":
args := c.RemainingArgs()
if len(args) != 0 {
return d, c.ArgErr()
}
d.hmac = true
case "merge":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
if args[0] != mergeMerge && args[0] != mergeSquash && args[0] != mergeRebase {
return d, c.Errf("invalid strategy: %s", args[0])
}
d.strategy = args[0]
case "user":
args := c.RemainingArgs()
if len(args) != 1 {
return d, c.ArgErr()
}
d.user = args[0]
case "env":
args := c.RemainingArgs()
if len(args) != 2 {
return d, c.ArgErr()
}
d.env[args[0]] = args[1]
}
}
}
return d, nil
}
const (
mergeMerge = "merge"
mergeSquash = "squash"
mergeRebase = "rebase"
)