added target binary move

This commit is contained in:
Darko Luketic 2023-12-01 23:37:36 +01:00
parent 8b56633711
commit 039a8cba64

View File

@ -4,6 +4,7 @@ import (
"code.icod.de/dalu/gomanager/ent"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
@ -42,9 +43,6 @@ func (r *Runner) Run() error {
if e != nil {
return e
}
if e := chownBinary(m.User, m.Group, fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath)); e != nil {
return e
}
_, e = r.client.Logentry.
Create().
SetContent(l).
@ -52,6 +50,27 @@ func (r *Runner) Run() error {
if e != nil {
return e
}
if m.MoveToTarget {
if fl, e := copyFile(
fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath),
fmt.Sprintf("%s/%s", m.RootPath, m.BinaryTargetPath),
); e != nil {
return fmt.Errorf("error copying file to target: %s", e)
} else {
_, e = r.client.Logentry.
Create().
SetContent(fl).
Save(context.Background())
if e != nil {
return e
}
}
} else {
if e := chownBinary(m.User, m.Group, fmt.Sprintf("%s/%s", m.RootPath, m.BinaryPath)); e != nil {
return e
}
}
}
}
return nil
@ -109,3 +128,21 @@ func chownBinary(user, group, file string) error {
cmd := exec.Command("chown", fmt.Sprintf("%s:%s", user, group), file)
return cmd.Run()
}
func copyFile(src, dst string) (string, error) {
srcF, e := os.Open(src)
if e != nil {
return "", e
}
defer srcF.Close()
dstF, e := os.Open(dst)
if e != nil {
return "", e
}
defer dstF.Close()
if size, e := io.Copy(dstF, srcF); e != nil {
return "", e
} else {
return fmt.Sprintf("copied %d bytes from %s to %s", size, src, dst), nil
}
}