From f91cd6e28e7808453485e6bdc2b054421578cf3c Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Thu, 12 Aug 2021 16:48:38 +0200 Subject: [PATCH] mkdir action, regexMatches --- config.yml | 20 ++++---------------- main.go | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/config.yml b/config.yml index 82dc9e5..6946850 100644 --- a/config.yml +++ b/config.yml @@ -48,21 +48,9 @@ directories: - path: /tmp/foo recursive: false events: ["CLOSE_WRITE", "MOVED_TO"] - regex: "/[^\\.][^/]+.(png|jpe?g|pdf)$" + regex: "/(\\d{2})(\\d{2})(\\d{2})[^/]*.(png|jpe?g|pdf)$" actions: - - type: http - method: post - url: http://localhost:8080/api/v1/_/renz_einfo/operation-file - header: - Token: Ksi02vcaHHasd1sjYxiq4J - Content-Type: application/json - content: | - { - "filename": "{{ filename }}" - } - onError: - - type: move - to: /tmp/error/{{ filename }} - stop: true - - type: log + content: "{{ filename }} {{ regexMatches.1 }} {{ regexMatches.2 }} {{ regexMatches.3 }} {{ regexMatches.4 }}" + - type: mkdir + path: /tmp/testdir/{{ regexMatches.1 }}/{{ regexMatches.2 }}/{{ regexMatches.3 }}/{{ regexMatches.4 }} diff --git a/main.go b/main.go index 01808ee..ee07430 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,9 @@ type ActionConfig struct { // move To *string + // mdir + Path *string + // mail // To *string Subject *string @@ -42,6 +45,8 @@ type ActionConfig struct { Stop bool OnSuccess []ActionConfig `yaml:"onSuccess"` OnError []ActionConfig `yaml:"onError"` + + _dirConf *DirectoryConfig } type DirectoryConfig struct { @@ -51,6 +56,7 @@ type DirectoryConfig struct { PollingInterval *int64 `yaml:"pollingInterval"` Regex *string _regex *regexp.Regexp + _regexMatches []string Actions []ActionConfig } @@ -163,8 +169,12 @@ func startWatcher(directory *DirectoryConfig) { eventStr = "POLLING" } log.Printf("path: %s; event: %s\n", p, eventStr) - if directory._regex == nil || directory._regex.MatchString(p) { + if directory._regex != nil { + directory._regexMatches = directory._regex.FindStringSubmatch(p) + } + if directory._regex == nil || directory._regexMatches != nil { for _, action := range directory.Actions { + action._dirConf = directory contin, _ := runAction(&action, eventInfo, nil) if !contin { break @@ -216,6 +226,8 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) err = actionHttp(action, eventInfo, ctx) case "move": err = actionMove(action, eventInfo, ctx) + case "mkdir": + err = actionMkDir(action, eventInfo, ctx) case "delete": err = actionDelete(action, eventInfo, ctx) case "log": @@ -231,6 +243,7 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) log.Printf("path: %s; action: %s; error %s", eventInfo.Path(), action.Id, err) for _, aE := range action.OnError { + aE._dirConf = action._dirConf _c, _ := runAction(&aE, eventInfo, ctx) contin = contin && _c if !contin { @@ -239,6 +252,7 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) } } else { for _, aS := range action.OnSuccess { + aS._dirConf = action._dirConf _c, errS := runAction(&aS, eventInfo, ctx) contin = contin && _c if errS != nil { @@ -348,10 +362,26 @@ func actionDelete(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCt return os.Remove(eventInfo.Path()) } +func actionMkDir(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) error { + if action.Path == nil { + return fmt.Errorf(("missing path: for mkdir action")) + } + + path, err := tpl2String(*action.Path, action, eventInfo, ctx) + if err != nil { + return err + } + return os.MkdirAll(path, 0755) +} + func actionLog(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) error { var logstr string if action.Content != nil { - logstr = *action.Content + var err error + logstr, err = tpl2String(*action.Content, action, eventInfo, ctx) + if err != nil { + return err + } } else { logstr = eventInfo.Path() } @@ -415,12 +445,13 @@ func tpl2String(tpl string, action *ActionConfig, eventInfo notify.EventInfo, ct p := eventInfo.Path() return _tpl.Execute(pongo2.Context{ - "path": p, - "dirname": filepath.Dir(p), - "filename": filepath.Base(p), - "event": eventInfo.Event().String(), - "action": action, - "context": ctx, + "path": p, + "dirname": filepath.Dir(p), + "filename": filepath.Base(p), + "event": eventInfo.Event().String(), + "action": action, + "regexMatches": action._dirConf._regexMatches, + "context": ctx, }) }