filepath.Walk is overkill here
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
6b5323da11
commit
eba6f5c188
35
backup.go
35
backup.go
|
@ -4,8 +4,9 @@ package virtbuf
|
||||||
// kick this out in 30 seconds
|
// kick this out in 30 seconds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
@ -14,35 +15,29 @@ func backupFiles(srcDir string, destDir string) error {
|
||||||
// Create the destination directory
|
// Create the destination directory
|
||||||
err := os.MkdirAll(destDir, os.ModePerm)
|
err := os.MkdirAll(destDir, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create directory: %v", err)
|
return errors.New(fmt.Sprintf("Failed to create directory: %v", err))
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walk through the source directory
|
// Read the contents of the source directory
|
||||||
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
|
entries, err := os.ReadDir(srcDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.New(fmt.Sprintf("Failed to read directory: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip if it's not a .test file or if it's a directory
|
// Iterate over the entries in the source directory
|
||||||
// if filepath.Ext(path) != ".json" || info.IsDir() {
|
for _, entry := range entries {
|
||||||
if info.IsDir() {
|
// Skip directories and files that do not have the .test extension
|
||||||
return nil
|
if entry.IsDir() || filepath.Ext(entry.Name()) != ".test" {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destination file path
|
srcPath := filepath.Join(srcDir, entry.Name())
|
||||||
destPath := filepath.Join(destDir, info.Name())
|
destPath := filepath.Join(destDir, entry.Name())
|
||||||
|
|
||||||
// Copy the file
|
// Copy the file
|
||||||
if err := copyFile(path, destPath); err != nil {
|
if err := copyFile(srcPath, destPath); err != nil {
|
||||||
return err
|
return errors.New(fmt.Sprintf("Failed to copy file %s: %v", entry.Name(), err))
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to copy files: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package virtbuf
|
||||||
|
|
||||||
|
// thank chatgpt for this because why. why write this if you can have it
|
||||||
|
// kick this out in 30 seconds
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func backupDir(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
|
||||||
|
}
|
Loading…
Reference in New Issue