command action

This commit is contained in:
Sebastian Frank 2021-12-22 12:04:24 +01:00
parent 4721baa00b
commit b3b1ad87b9
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57
2 changed files with 27 additions and 1 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
tmp/ tmp/
fstrigger

25
main.go
View File

@ -8,6 +8,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -45,6 +46,10 @@ type ActionConfig struct {
Body *string Body *string
From *string From *string
// command
Command *string
Args []string
Stop bool Stop bool
OnSuccess []ActionConfig `yaml:"onSuccess"` OnSuccess []ActionConfig `yaml:"onSuccess"`
OnError []ActionConfig `yaml:"onError"` OnError []ActionConfig `yaml:"onError"`
@ -256,6 +261,8 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx)
err = actionLog(action, eventInfo, ctx) err = actionLog(action, eventInfo, ctx)
case "mail": case "mail":
err = actionMail(action, eventInfo, ctx) err = actionMail(action, eventInfo, ctx)
case "command":
err = actionCommand(action, eventInfo, ctx)
default: default:
err = fmt.Errorf("action type %s unknown", action.Type) err = fmt.Errorf("action type %s unknown", action.Type)
} }
@ -506,6 +513,24 @@ func actionMail(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx)
return mail.Send() return mail.Send()
} }
func actionCommand(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) error {
if action.Command == nil {
return fmt.Errorf("missing command")
}
args := make([]string, len(action.Args))
for idx, a := range action.Args {
var err error
args[idx], err = tpl2String(a, action, eventInfo, ctx)
if err != nil {
return err
}
}
out, err := exec.Command(*action.Command, action.Args...).Output()
log.Println("COMMAND OUTPUT: ", string(out))
return err
}
func tpl2String(tpl string, action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) (string, error) { func tpl2String(tpl string, action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) (string, error) {
_tpl, err := pongo2.FromString(tpl) _tpl, err := pongo2.FromString(tpl)
if err != nil { if err != nil {