mkdir action, regexMatches
This commit is contained in:
parent
da1322b7cb
commit
f91cd6e28e
20
config.yml
20
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 }}
|
||||
|
35
main.go
35
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()
|
||||
}
|
||||
@ -420,6 +450,7 @@ func tpl2String(tpl string, action *ActionConfig, eventInfo notify.EventInfo, ct
|
||||
"filename": filepath.Base(p),
|
||||
"event": eventInfo.Event().String(),
|
||||
"action": action,
|
||||
"regexMatches": action._dirConf._regexMatches,
|
||||
"context": ctx,
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user