wanna-goon/recover.go
2024-09-21 07:22:23 -07:00

60 lines
2.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Merith-TK/utils/debug"
)
// restoreFiles restores files from a backup directory to their original locations.
// It sets the debug title to "Restor" and retrieves the user's profile directory.
// If the USERPROFILE environment variable is not set, it returns an error.
// It then constructs the path to the "wanna-goon" directory within the user's profile.
// For each folder in the "wanna-goon" directory, it iterates over subfolders and moves them
// to their respective drive letters. For example, "wanna-goon/C/Users/" gets moved to "C:/Users/"
// and "wanna-goon/E/Files/" gets moved to "E:/Files/".
func restoreFiles() error {
debug.SetTitle("Restore")
defer debug.ResetTitle()
userProfile := os.Getenv("USERPROFILE")
if userProfile == "" {
return fmt.Errorf("could not get USERPROFILE environment variable")
}
wannaGoonPath, wannaGoonErr := filepath.Abs("C:/.wanna-goon")
if wannaGoonErr != nil {
return fmt.Errorf("could not get absolute path for .wanna-goon: %v", wannaGoonErr)
}
err := filepath.Walk(wannaGoonPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
destPath := filepath.Join("C:/", strings.TrimPrefix(path, wannaGoonPath))
debug.Print("creating directory:", destPath)
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
return fmt.Errorf("could not create directory %s: %v", destPath, err)
}
} else {
destPath := filepath.Join("C:/", strings.TrimPrefix(path, wannaGoonPath))
debug.Print("moving file:", path, "to:", destPath)
if err := os.Rename(path, destPath); err != nil {
return fmt.Errorf("could not move file %s to %s: %v", path, destPath, err)
}
}
return nil
})
if err != nil {
return fmt.Errorf("error restoring files: %v", err)
} else {
debug.Print("files restored successfully")
if err := os.RemoveAll(wannaGoonPath); err != nil {
return fmt.Errorf("could not remove wanna-goon directory: %v", err)
}
}
return nil
}