From ff5873930fd5b3078ca71bd06b8560150ac89d91 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 4 May 2022 23:13:06 +0200 Subject: [PATCH] Add Variable replace possibility Solve https://github.com/abiosoft/caddy-exec/issues/7 --- .gitignore | 4 ++++ README.md | 5 +++-- middleware.go | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9464ba6..a0eaeb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ caddy buildenv_* + +# vscode +*vscode +*.code-workspace \ No newline at end of file diff --git a/README.md b/README.md index 99f2a53..cf407a7 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,9 @@ As an handler within a route. "handler": "exec", // command to execute "command": "git", - // command arguments - "args": ["pull", "origin", "master"], + // command arguments it's also possible to use + // caddy variables like {http.request.uuid} + "args": ["pull", "origin", "master", "# {http.request.uuid}"], // [optional] directory to run the command from. Default is the current directory. "directory": "/home/user/site/public", diff --git a/middleware.go b/middleware.go index ebe5be1..5493eb0 100644 --- a/middleware.go +++ b/middleware.go @@ -45,6 +45,12 @@ func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy Error string `json:"error,omitempty"` } + repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) + + for index, argument := range m.Args { + m.Args[index] = repl.ReplaceAll(argument, "") + } + err := m.run() if err == nil {