summaryrefslogtreecommitdiff
path: root/backup.go
blob: df2e09855f5af598a6462830f40b2b7bc88e883a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package virtbuf

// thank chatgpt for this because why. why write this if you can have it
// kick this out in 30 seconds

import (
	"io"
	"log"
	"os"
	"path/filepath"
)

func backupFiles(srcDir string, destDir string) error {
	// Create the destination directory
	err := os.MkdirAll(destDir, os.ModePerm)
	if err != nil {
		log.Println("Failed to create directory: %v", err)
		return err
	}

	// Walk through the source directory
	err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// Skip if it's not a .test file or if it's a directory
		// if filepath.Ext(path) != ".json" || info.IsDir() {
		if info.IsDir() {
			return nil
		}

		// Destination file path
		destPath := filepath.Join(destDir, info.Name())

		// Copy the file
		if err := copyFile(path, destPath); err != nil {
			return err
		}
		return nil
	})

	if err != nil {
		log.Println("Failed to copy files: %v", err)
		return err
	}
	return nil
}

// copyFile copies a file from src to dest
func copyFile(src, dest string) error {
	srcFile, err := os.Open(src)
	if err != nil {
		return err
	}
	defer srcFile.Close()

	destFile, err := os.Create(dest)
	if err != nil {
		return err
	}
	defer destFile.Close()

	// Copy the content
	_, err = io.Copy(destFile, srcFile)
	return err
}