-
Notifications
You must be signed in to change notification settings - Fork 14
/
caddyfile.go
200 lines (182 loc) · 4.83 KB
/
caddyfile.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package command
import (
"encoding/json"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func init() {
httpcaddyfile.RegisterGlobalOption("exec", parseGlobalCaddyfileBlock)
httpcaddyfile.RegisterHandlerDirective("exec", parseHandlerCaddyfileBlock)
}
func newCommandFromDispenser(d *caddyfile.Dispenser) (cmd Cmd, err error) {
cmd.UnmarshalCaddyfile(d)
return
}
// parseHandlerCaddyfileBlock configures the handler directive from Caddyfile.
// Syntax:
//
// exec [<matcher>] [<command> [<args...>]] {
// command <text>
// args <text>...
// directory <text>
// timeout <duration>
// log <log output module>
// err_log <log output module>
// foreground
// pass_thru
// startup
// shutdown
// }
func parseHandlerCaddyfileBlock(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
cmd, err := newCommandFromDispenser(h.Dispenser)
return Middleware{Cmd: cmd}, err
}
// parseGlobalCaddyfileBlock configures the "exec" global option from Caddyfile.
// Syntax:
//
// exec [<command> [<args...>]] {
// command <text>...
// args <text>...
// directory <text>
// timeout <duration>
// log <log output module>
// err_log <log output module>
// foreground
// pass_thru
// startup
// shutdown
// }
func parseGlobalCaddyfileBlock(d *caddyfile.Dispenser, prev interface{}) (interface{}, error) {
var exec App
// decode the existing value and merge to it.
if prev != nil {
if app, ok := prev.(httpcaddyfile.App); ok {
if err := json.Unmarshal(app.Value, &exec); err != nil {
return nil, d.Errf("internal error: %v", err)
}
}
}
cmd, err := newCommandFromDispenser(d)
if err != nil {
return nil, err
}
// global block commands are not necessarily bound to a route,
// should default to running at startup.
if len(cmd.At) == 0 {
cmd.At = append(cmd.At, "startup")
}
// append command to global exec app.
exec.Commands = append(exec.Commands, cmd)
// tell Caddyfile adapter that this is the JSON for an app
return httpcaddyfile.App{
Name: "exec",
Value: caddyconfig.JSON(exec, nil),
}, nil
}
// UnmarshalCaddyfile configures the handler directive from Caddyfile.
// Syntax:
//
// exec [<matcher>] [<command> [<args...>]] {
// command <text>
// args <text>...
// directory <text>
// timeout <duration>
// log <log output module>
// err_log <log output module>
// foreground
// pass_thru
// startup
// shutdown
// }
func (c *Cmd) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// consume "exec", then grab the command, if present.
if d.NextArg() && d.NextArg() {
c.Command = d.Val()
}
// everything else are args, if present.
c.Args = d.RemainingArgs()
// parse the next block
return c.unmarshalBlock(d)
}
func (c *Cmd) unmarshalBlock(d *caddyfile.Dispenser) error {
for d.NextBlock(0) {
switch d.Val() {
case "command":
if c.Command != "" {
return d.Err("command specified twice")
}
if !d.Args(&c.Command) {
return d.ArgErr()
}
c.Args = d.RemainingArgs()
case "args":
if len(c.Args) > 0 {
return d.Err("args specified twice")
}
c.Args = d.RemainingArgs()
case "directory":
if !d.Args(&c.Directory) {
return d.ArgErr()
}
case "foreground":
c.Foreground = true
case "pass_thru":
c.PassThru = true
case "startup":
c.At = append(c.At, "startup")
case "shutdown":
c.At = append(c.At, "shutdown")
case "timeout":
if !d.Args(&c.Timeout) {
return d.ArgErr()
}
case "log":
rawMessage, err := c.unmarshalLog(d)
if err != nil {
return err
}
c.StdWriterRaw = rawMessage
case "err_log":
rawMessage, err := c.unmarshalLog(d)
if err != nil {
return err
}
c.ErrWriterRaw = rawMessage
default:
return d.Errf("'%s' not expected", d.Val())
}
}
return nil
}
func (c *Cmd) unmarshalLog(d *caddyfile.Dispenser) (json.RawMessage, error) {
if !d.NextArg() {
return nil, d.ArgErr()
}
moduleName := d.Val()
// copied from caddy's source
// TODO: raise the topic of log re-use by non-standard modules.
var wo caddy.WriterOpener
switch moduleName {
case "stdout":
wo = caddy.StdoutWriter{}
case "stderr":
wo = caddy.StderrWriter{}
case "discard":
wo = caddy.DiscardWriter{}
default:
modID := "caddy.logging.writers." + moduleName
unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
return nil, err
}
var ok bool
wo, ok = unm.(caddy.WriterOpener)
if !ok {
return nil, d.Errf("module %s (%T) is not a WriterOpener", modID, unm)
}
}
return caddyconfig.JSONModuleObject(wo, "output", moduleName, nil), nil
}