copy action with chmod

This commit is contained in:
Sebastian Frank 2021-12-22 10:50:15 +01:00
parent e15541d3fa
commit 4721baa00b
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57

49
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
@ -31,8 +32,9 @@ type ActionConfig struct {
Content *string
Header map[string]string
// move
To *string
// move, copy
To *string
FileMode *os.FileMode
// mdir
Path *string
@ -244,6 +246,8 @@ func runAction(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx)
err = actionHttp(action, eventInfo, ctx)
case "move":
err = actionMove(action, eventInfo, ctx)
case "copy":
err = actionCopy(action, eventInfo, ctx)
case "mkdir":
err = actionMkDir(action, eventInfo, ctx)
case "delete":
@ -379,6 +383,47 @@ func actionMove(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx)
}
err = os.Rename(eventInfo.Path(), to)
if err != nil {
return err
}
if action.FileMode != nil {
err = os.Chmod(to, *action.FileMode)
}
return err
}
func actionCopy(action *ActionConfig, eventInfo notify.EventInfo, ctx actionCtx) error {
if action.To == nil {
return fmt.Errorf("missing to: as destination for copy action")
}
to, err := tpl2String(*action.To, action, eventInfo, ctx)
if err != nil {
return err
}
in, err := os.Open(eventInfo.Path())
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(to)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
if action.FileMode != nil {
err = os.Chmod(to, *action.FileMode)
}
return err
}