summaryrefslogtreecommitdiffstats
path: root/pkg/common/file.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-20 23:07:42 +0200
committerDaniel Baumann <daniel@debian.org>2024-11-09 15:38:42 +0100
commit714c83b2736d7e308bc33c49057952490eb98be2 (patch)
tree1d9ba7035798368569cd49056f4d596efc908cd8 /pkg/common/file.go
parentInitial commit. (diff)
downloadforgejo-act-debian.tar.xz
forgejo-act-debian.zip
Adding upstream version 1.21.4.HEADupstream/1.21.4upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'pkg/common/file.go')
-rw-r--r--pkg/common/file.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/pkg/common/file.go b/pkg/common/file.go
new file mode 100644
index 0000000..09c2102
--- /dev/null
+++ b/pkg/common/file.go
@@ -0,0 +1,73 @@
+package common
+
+import (
+ "fmt"
+ "io"
+ "os"
+)
+
+// CopyFile copy file
+func CopyFile(source string, dest string) (err error) {
+ sourcefile, err := os.Open(source)
+ if err != nil {
+ return err
+ }
+
+ defer sourcefile.Close()
+
+ destfile, err := os.Create(dest)
+ if err != nil {
+ return err
+ }
+
+ defer destfile.Close()
+
+ _, err = io.Copy(destfile, sourcefile)
+ if err == nil {
+ sourceinfo, err := os.Stat(source)
+ if err != nil {
+ _ = os.Chmod(dest, sourceinfo.Mode())
+ }
+ }
+
+ return
+}
+
+// CopyDir recursive copy of directory
+func CopyDir(source string, dest string) (err error) {
+ // get properties of source dir
+ sourceinfo, err := os.Stat(source)
+ if err != nil {
+ return err
+ }
+
+ // create dest dir
+
+ err = os.MkdirAll(dest, sourceinfo.Mode())
+ if err != nil {
+ return err
+ }
+
+ objects, err := os.ReadDir(source)
+
+ for _, obj := range objects {
+ sourcefilepointer := source + "/" + obj.Name()
+
+ destinationfilepointer := dest + "/" + obj.Name()
+
+ if obj.IsDir() {
+ // create sub-directories - recursively
+ err = CopyDir(sourcefilepointer, destinationfilepointer)
+ if err != nil {
+ fmt.Println(err)
+ }
+ } else {
+ // perform copy
+ err = CopyFile(sourcefilepointer, destinationfilepointer)
+ if err != nil {
+ fmt.Println(err)
+ }
+ }
+ }
+ return err
+}