From 039a8cba64aa9deb0e233bf1e5f3121ea77e7c12 Mon Sep 17 00:00:00 2001 From: Darko Luketic Date: Fri, 1 Dec 2023 23:37:36 +0100 Subject: [PATCH] added target binary move --- runner/runner.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/runner/runner.go b/runner/runner.go index 7f53980..3008ac2 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -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 + } +}