2024-10-27 06:05:49 -05:00
|
|
|
package virtbuf
|
|
|
|
|
|
|
|
// thank chatgpt for this because why. why write this if you can have it
|
|
|
|
// kick this out in 30 seconds
|
|
|
|
|
|
|
|
import (
|
2024-10-27 07:20:47 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-10-27 06:05:49 -05:00
|
|
|
"io"
|
2024-10-27 07:49:03 -05:00
|
|
|
"log"
|
2024-10-27 06:05:49 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func backupFiles(srcDir string, destDir string) error {
|
|
|
|
// Create the destination directory
|
|
|
|
err := os.MkdirAll(destDir, os.ModePerm)
|
|
|
|
if err != nil {
|
2024-10-27 07:20:47 -05:00
|
|
|
return errors.New(fmt.Sprintf("Failed to create directory: %v", err))
|
2024-10-27 06:05:49 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:20:47 -05:00
|
|
|
// Read the contents of the source directory
|
|
|
|
entries, err := os.ReadDir(srcDir)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Failed to read directory: %v", err))
|
|
|
|
}
|
2024-10-27 06:05:49 -05:00
|
|
|
|
2024-10-27 07:20:47 -05:00
|
|
|
// Iterate over the entries in the source directory
|
|
|
|
for _, entry := range entries {
|
|
|
|
// Skip directories and files that do not have the .test extension
|
2024-10-27 07:49:03 -05:00
|
|
|
if entry.IsDir() {
|
2024-10-27 07:20:47 -05:00
|
|
|
continue
|
2024-10-27 06:05:49 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:49:03 -05:00
|
|
|
log.Println("backing up file", entry.Name())
|
2024-10-27 07:20:47 -05:00
|
|
|
srcPath := filepath.Join(srcDir, entry.Name())
|
|
|
|
destPath := filepath.Join(destDir, entry.Name())
|
2024-10-27 06:05:49 -05:00
|
|
|
|
|
|
|
// Copy the file
|
2024-10-27 07:20:47 -05:00
|
|
|
if err := copyFile(srcPath, destPath); err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Failed to copy file %s: %v", entry.Name(), err))
|
2024-10-27 06:05:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|