mkdir action, regexMatches

This commit is contained in:
Sebastian Frank 2021-08-12 16:48:38 +02:00
parent da1322b7cb
commit f91cd6e28e
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57
2 changed files with 43 additions and 24 deletions

View File

@ -48,21 +48,9 @@ directories:
- path: /tmp/foo - path: /tmp/foo
recursive: false recursive: false
events: ["CLOSE_WRITE", "MOVED_TO"] events: ["CLOSE_WRITE", "MOVED_TO"]
regex: "/[^\\.][^/]+.(png|jpe?g|pdf)$" regex: "/(\\d{2})(\\d{2})(\\d{2})[^/]*.(png|jpe?g|pdf)$"
actions: 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 - 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 }}

35
main.go
View File

@ -33,6 +33,9 @@ type ActionConfig struct {
// move // move
To *string To *string
// mdir
Path *string
// mail // mail
// To *string // To *string
Subject *string Subject *string
@ -42,6 +45,8 @@ type ActionConfig struct {
Stop bool Stop bool
OnSuccess []ActionConfig `yaml:"onSuccess"` OnSuccess []ActionConfig `yaml:"onSuccess"`
OnError []ActionConfig `yaml:"onError"` OnError []ActionConfig `yaml:"onError"`
_dirConf *DirectoryConfig
} }
type DirectoryConfig struct { type DirectoryConfig struct {
@ -51,6 +56,7 @@ type DirectoryConfig struct {
PollingInterval *int64 `yaml:"pollingInterval"` PollingInterval *int64 `yaml:"pollingInterval"`
Regex *string Regex *string
_regex *regexp.Regexp _regex *regexp.Regexp
_regexMatches []string
Actions []ActionConfig Actions []ActionConfig
} }
@ -163,8 +169,12 @@ func startWatcher(directory *DirectoryConfig) {
eventStr = "POLLING" eventStr = "POLLING"
} }
log.Printf("path: %s; event: %s\n", p, eventStr) 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 { for _, action := range directory.Actions {
action._dirConf = directory
contin, _ := runAction(&action, eventInfo, nil) contin, _ := runAction(&action, eventInfo, nil)
if !contin { if !contin {
break break
@ -216,6 +226,8 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx)
err = actionHttp(action, eventInfo, ctx) err = actionHttp(action, eventInfo, ctx)
case "move": case "move":
err = actionMove(action, eventInfo, ctx) err = actionMove(action, eventInfo, ctx)
case "mkdir":
err = actionMkDir(action, eventInfo, ctx)
case "delete": case "delete":
err = actionDelete(action, eventInfo, ctx) err = actionDelete(action, eventInfo, ctx)
case "log": 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) log.Printf("path: %s; action: %s; error %s", eventInfo.Path(), action.Id, err)
for _, aE := range action.OnError { for _, aE := range action.OnError {
aE._dirConf = action._dirConf
_c, _ := runAction(&aE, eventInfo, ctx) _c, _ := runAction(&aE, eventInfo, ctx)
contin = contin && _c contin = contin && _c
if !contin { if !contin {
@ -239,6 +252,7 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx)
} }
} else { } else {
for _, aS := range action.OnSuccess { for _, aS := range action.OnSuccess {
aS._dirConf = action._dirConf
_c, errS := runAction(&aS, eventInfo, ctx) _c, errS := runAction(&aS, eventInfo, ctx)
contin = contin && _c contin = contin && _c
if errS != nil { if errS != nil {
@ -348,10 +362,26 @@ func actionDelete(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCt
return os.Remove(eventInfo.Path()) 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 { func actionLog(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) error {
var logstr string var logstr string
if action.Content != nil { if action.Content != nil {
logstr = *action.Content var err error
logstr, err = tpl2String(*action.Content, action, eventInfo, ctx)
if err != nil {
return err
}
} else { } else {
logstr = eventInfo.Path() logstr = eventInfo.Path()
} }
@ -420,6 +450,7 @@ func tpl2String(tpl string, action *ActionConfig, eventInfo notify.EventInfo, ct
"filename": filepath.Base(p), "filename": filepath.Base(p),
"event": eventInfo.Event().String(), "event": eventInfo.Event().String(),
"action": action, "action": action,
"regexMatches": action._dirConf._regexMatches,
"context": ctx, "context": ctx,
}) })
} }