From b3b1ad87b9fb000455742fcf49dba8126f89add2 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Wed, 22 Dec 2021 12:04:24 +0100 Subject: [PATCH] command action --- .gitignore | 3 ++- main.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c036379..c9bb797 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -tmp/ \ No newline at end of file +tmp/ +fstrigger diff --git a/main.go b/main.go index 94fbc15..1fb2957 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "os" + "os/exec" "path/filepath" "regexp" "strings" @@ -45,6 +46,10 @@ type ActionConfig struct { Body *string From *string + // command + Command *string + Args []string + Stop bool OnSuccess []ActionConfig `yaml:"onSuccess"` OnError []ActionConfig `yaml:"onError"` @@ -256,6 +261,8 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) err = actionLog(action, eventInfo, ctx) case "mail": err = actionMail(action, eventInfo, ctx) + case "command": + err = actionCommand(action, eventInfo, ctx) default: 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() } +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) { _tpl, err := pongo2.FromString(tpl) if err != nil {