Skip to content

Commit

Permalink
Merge pull request #21 from mindon/master
Browse files Browse the repository at this point in the history
variable is always bounded to the first request #20
  • Loading branch information
abiosoft authored Jun 3, 2024
2 parents 46a1d88 + 1665c00 commit a42a5b2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Cmd struct {
Command string `json:"command,omitempty"`

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

// The directory to run the command from.
// Defaults to current directory.
Expand Down
5 changes: 4 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ 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)

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

err := m.run()

Expand Down Expand Up @@ -75,5 +77,6 @@ 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
}
6 changes: 3 additions & 3 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ 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.Args...))
cmdInfo := zap.Any("command", append([]string{c.Command}, c.Argv...))
log := c.log.With(cmdInfo)
startTime := time.Now()

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

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

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

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

// configure command
Expand Down

0 comments on commit a42a5b2

Please sign in to comment.