Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,27 @@ func (a *App) Provision(ctx caddy.Context) error {
}

a.log = ctx.Logger(a)
repl := caddy.NewReplacer()
for _, cmd := range a.Commands {
if err := cmd.provision(ctx, a); err != nil {
return err
}
a.addCmd(cmd)
}
return nil
}

func (a *App) addCmd(c Cmd) {
runner := runnerFunc(c.run)
for at := range c.at {
a.commands[at] = append(a.commands[at], runner)
// replace global placeholders
argv := make([]string, len(cmd.Args))
for index, argument := range cmd.Args {
argv[index] = repl.ReplaceAll(argument, "")
}

runner := runnerFunc(func() error {
return cmd.run(argv)
})

for at := range cmd.at {
a.commands[at] = append(a.commands[at], runner)
}
}
return nil
}

// Validate implements caddy.Validator
Expand Down
3 changes: 1 addition & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type Cmd struct {
Command string `json:"command,omitempty"`

// The command args.
Args []string
Argv []string `json:"args,omitempty"`
Args []string `json:"args,omitempty"`

// The directory to run the command from.
// Defaults to current directory.
Expand Down
5 changes: 2 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func (m Middleware) Validate() error { return m.Cmd.validate() }
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)

// replace per-request placeholders
argv := make([]string, len(m.Args))
for index, argument := range m.Args {
argv[index] = repl.ReplaceAll(argument, "")
}
m.Argv = argv

err := m.run()
err := m.run(argv)

if m.PassThru {
if err != nil {
Expand Down Expand Up @@ -77,6 +77,5 @@ func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy
// Cleanup implements caddy.Cleanup
// TODO: ensure all running processes are terminated.
func (m *Middleware) Cleanup() error {
m.Argv = nil
return nil
}
8 changes: 4 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type runnerFunc func() error

func (r runnerFunc) Run() error { return r() }

func (c *Cmd) run() error {
cmdInfo := zap.Any("command", append([]string{c.Command}, c.Argv...))
func (c *Cmd) run(args []string) error {
cmdInfo := zap.Any("command", append([]string{c.Command}, args...))
log := c.log.With(cmdInfo)
startTime := time.Now()

cmd := exec.Command(c.Command, c.Argv...)
cmd := exec.Command(c.Command, args...)

done := make(chan struct{}, 1)

Expand All @@ -36,7 +36,7 @@ func (c *Cmd) run() error {
cancel()
}()

cmd = exec.CommandContext(ctx, c.Command, c.Argv...)
cmd = exec.CommandContext(ctx, c.Command, args...)
}

// configure command
Expand Down