mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-11 16:14:52 +00:00
#162 do use temporary files, provide env variable
This commit is contained in:
parent
78aa85e0c1
commit
213e4529e8
2 changed files with 25 additions and 19 deletions
15
hook/hook.go
15
hook/hook.go
|
@ -13,6 +13,7 @@ import (
|
|||
"log"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
@ -268,7 +269,6 @@ type Argument struct {
|
|||
Source string `json:"source,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
EnvName string `json:"envname,omitempty"`
|
||||
FileName string `json:"filename,omitempty"`
|
||||
Base64Decode bool `json:"base64decode,omitempty"`
|
||||
DeleteOnExit bool `json:"deleteOnExit,omitempty"`
|
||||
}
|
||||
|
@ -497,7 +497,8 @@ func (h *Hook) ExtractCommandArgumentsForEnv(headers, query, payload *map[string
|
|||
|
||||
// FileParameter describes a pass-file-to-command instance to be stored as file
|
||||
type FileParameter struct {
|
||||
Filename string
|
||||
File *os.File
|
||||
EnvName string
|
||||
Data []byte
|
||||
DeleteOnExit bool
|
||||
}
|
||||
|
@ -511,10 +512,10 @@ func (h *Hook) ExtractCommandArgumentsForFile(headers, query, payload *map[strin
|
|||
for i := range h.PassFileToCommand {
|
||||
if arg, ok := h.PassFileToCommand[i].Get(headers, query, payload); ok {
|
||||
|
||||
if h.PassFileToCommand[i].FileName == "" {
|
||||
// if no filename is set, fall-back on the name
|
||||
log.Printf("no filename specified, falling back to [%s]", EnvNamespace+h.PassFileToCommand[i].Name)
|
||||
h.PassFileToCommand[i].FileName = EnvNamespace + h.PassFileToCommand[i].Name
|
||||
if h.PassFileToCommand[i].EnvName == "" {
|
||||
// if no environment-variable name is set, fall-back on the name
|
||||
log.Printf("no ENVVAR name specified, falling back to [%s]", EnvNamespace+strings.ToUpper(h.PassFileToCommand[i].Name))
|
||||
h.PassFileToCommand[i].EnvName = EnvNamespace + strings.ToUpper(h.PassFileToCommand[i].Name)
|
||||
}
|
||||
|
||||
var fileContent []byte
|
||||
|
@ -528,7 +529,7 @@ func (h *Hook) ExtractCommandArgumentsForFile(headers, query, payload *map[strin
|
|||
fileContent = []byte(arg)
|
||||
}
|
||||
|
||||
args = append(args, FileParameter{Filename: h.PassFileToCommand[i].FileName, Data: fileContent, DeleteOnExit: h.PassFileToCommand[i].DeleteOnExit})
|
||||
args = append(args, FileParameter{EnvName: h.PassFileToCommand[i].EnvName, Data: fileContent, DeleteOnExit: h.PassFileToCommand[i].DeleteOnExit})
|
||||
|
||||
} else {
|
||||
errors = append(errors, &ArgumentError{h.PassFileToCommand[i]})
|
||||
|
|
29
webhook.go
29
webhook.go
|
@ -331,8 +331,6 @@ func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, b
|
|||
}
|
||||
}
|
||||
|
||||
cmd.Env = append(os.Environ(), envs...)
|
||||
|
||||
files, errors := h.ExtractCommandArgumentsForFile(headers, query, payload)
|
||||
|
||||
if errors != nil {
|
||||
|
@ -342,17 +340,24 @@ func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, b
|
|||
}
|
||||
|
||||
for i := range files {
|
||||
if h.CommandWorkingDirectory != "" {
|
||||
files[i].Filename = h.CommandWorkingDirectory + "/" + files[i].Filename
|
||||
tmpfile, err := ioutil.TempFile(h.CommandWorkingDirectory, files[i].EnvName)
|
||||
if err != nil {
|
||||
log.Printf("error creating temp file [%s]", err)
|
||||
}
|
||||
log.Printf("writing env %s file %s", files[i].EnvName, tmpfile.Name())
|
||||
if _, err := tmpfile.Write(files[i].Data); err != nil {
|
||||
log.Printf("error writing file %s [%s]", tmpfile.Name(), err)
|
||||
}
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
log.Printf("error closing file %s [%s]", tmpfile.Name(), err)
|
||||
}
|
||||
|
||||
log.Printf("writing file %s", files[i].Filename)
|
||||
err := ioutil.WriteFile(files[i].Filename, files[i].Data, 0644)
|
||||
if err != nil {
|
||||
log.Printf("error writing file %s [%s]", files[i].Filename, err)
|
||||
}
|
||||
files[i].File = tmpfile
|
||||
envs = append(envs, files[i].EnvName+"="+tmpfile.Name())
|
||||
}
|
||||
|
||||
cmd.Env = append(os.Environ(), envs...)
|
||||
|
||||
log.Printf("executing %s (%s) with arguments %q and environment %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
@ -365,10 +370,10 @@ func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, b
|
|||
|
||||
for i := range files {
|
||||
if files[i].DeleteOnExit {
|
||||
log.Printf("removing file: %s\n", files[i].Filename)
|
||||
err := os.Remove(files[i].Filename)
|
||||
log.Printf("removing file %s\n", files[i].File.Name())
|
||||
err := os.Remove(files[i].File.Name())
|
||||
if err != nil {
|
||||
log.Printf("error removing file %s [%s]", files[i].Filename, err)
|
||||
log.Printf("error removing file %s [%s]", files[i].File.Name(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue