diff --git a/README.md b/README.md index aa2acd0..1a02672 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Update the wowtools-cli.yml file if you have WoW installed in a custom location. ## Current Functionality 1. Creates `_retail_\Backups`, `_retail_\Backups\WTF` and `_retail_\Backups\ElvUI` directories if they don't exist 2. Zips up the WTF directory (`C:\Program Files (x86)\World of Warcraft\_retail_\WTF`) and backs it up to `C:\Program Files (x86)\World of Warcraft\_retail_\Backups\WTF`, with the format of YYYY-MM-DD.zip + 1. Reads the `retention_rate` from the yml and removes the oldest zip file if the count in the folder is higher than it. 3. Checks for any updates to ElvUI 1. If a newer version is found via the API, you will be asked if you want to update 2. If Yes diff --git a/cmd/removeOldestFile.go b/cmd/removeOldestFile.go new file mode 100644 index 0000000..5c15346 --- /dev/null +++ b/cmd/removeOldestFile.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "log" + "os" + "wowtools/utilities" + + "github.com/spf13/viper" +) + +func RemoveOldestWtfZip() { + retentionRate := viper.GetInt("retention_rate") + wtfBackupDir := viper.GetString("backup_dir") + "WTF\\" + fileCount := utilities.GetFileCount(wtfBackupDir) + if fileCount > retentionRate { + oldestFile := utilities.GetOldestFolder(wtfBackupDir) + os.Chdir(wtfBackupDir) + removeFile := os.Remove(oldestFile) + if removeFile != nil { + log.Fatal() + } + } +} diff --git a/cmd/updateElvUI.go b/cmd/updateElvUI.go index 3929562..2f9def8 100644 --- a/cmd/updateElvUI.go +++ b/cmd/updateElvUI.go @@ -25,6 +25,7 @@ func UpdateElvUI() { fmt.Printf("A later version of ElvUI is available. Current version: %s; New version: %s\n", stringCurrentVersion, latestVersion) updatePrompt := utilities.AskForConfirmation("Do you want to install the lastest version of ElvUI?") if updatePrompt { + removeOldElvuiZip() ZipElvUI() fmt.Printf("Downloading ElvUI %s\n", latestVersion) utilities.DownloadFiles(filename, downloadUri) @@ -59,3 +60,17 @@ func ZipElvUI() { } fmt.Println("Folder backup complete") } + +func removeOldElvuiZip() { + backupFolder := viper.GetString("backup_dir") + "ElvUI\\" + fileCount := utilities.GetFileCount(backupFolder) + fmt.Println(fileCount) + if fileCount > 2 { + oldestFile := utilities.GetOldestFolder(backupFolder) + os.Chdir(backupFolder) + removeFile := os.Remove(oldestFile) + if removeFile != nil { + log.Fatal() + } + } +} diff --git a/main.go b/main.go index f5dfb04..f523929 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ func main() { go utilities.VerifyFolders(viper.GetString("backup_dir")+"WTF", &wg) wg.Wait() + cmd.RemoveOldestWtfZip() cmd.WtfBackup() cmd.UpdateElvUI() utilities.OpenCurseforge() diff --git a/test/fileDate_test.go b/test/fileDate_test.go new file mode 100644 index 0000000..231015a --- /dev/null +++ b/test/fileDate_test.go @@ -0,0 +1,32 @@ +package test + +import ( + "fmt" + "io/fs" + "io/ioutil" + "log" + "os" + "testing" + "time" +) + +func TestCleanFolders(t *testing.T) { + t.Skip() + filepath := "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Backups\\WTF" + var oldestFile fs.FileInfo + files, err := ioutil.ReadDir(filepath) + if err != nil { + log.Fatal(err) + } + oldestTime := time.Now() + for _, file := range files { + if file.Mode().IsRegular() && file.ModTime().Before(oldestTime) { + oldestFile = file + oldestTime = file.ModTime() + } + } + if oldestFile == nil { + err = os.ErrNotExist + } + fmt.Println(oldestFile.Name()) +} diff --git a/test/removeFile_test.go b/test/removeFile_test.go new file mode 100644 index 0000000..e9bfb51 --- /dev/null +++ b/test/removeFile_test.go @@ -0,0 +1,31 @@ +package test + +import ( + "log" + "os" + "testing" + "wowtools/utilities" +) + +func TestRemoveFile(t *testing.T) { + t.Skip() + os.Chdir("C:\\Temp\\wowtools") + testFile, err := os.Create("Test.txt") + if err != nil { + log.Fatal() + } + t.Log(testFile) + testFile.Close() + + retentionRate := 1 + fileCount := utilities.GetFileCount("C:\\Temp\\wowtools\\") + t.Log(fileCount) + if fileCount > retentionRate { + oldestFile := utilities.GetOldestFolder("C:\\Temp\\wowtools") + t.Log(oldestFile) + removeFile := os.Remove(oldestFile) + if removeFile != nil { + log.Fatal() + } + } +} diff --git a/utilities/fileHelper.go b/utilities/fileHelper.go index 4ebd4b4..ffc185a 100644 --- a/utilities/fileHelper.go +++ b/utilities/fileHelper.go @@ -4,12 +4,15 @@ import ( "archive/zip" "fmt" "io" + "io/fs" + "io/ioutil" "log" "net/http" "os" "path/filepath" "strings" "sync" + "time" ) func ZipSource(source, target string) error { @@ -148,3 +151,28 @@ func VerifyFolders(filepath string, wg *sync.WaitGroup) { os.Mkdir(filepath, os.ModePerm) } } + +func GetOldestFolder(filepath string) string { + var oldestFile fs.FileInfo + files, err := ioutil.ReadDir(filepath) + if err != nil { + log.Fatal(err) + } + oldestTime := time.Now() + for _, file := range files { + if file.Mode().IsRegular() && file.ModTime().Before(oldestTime) { + oldestFile = file + oldestTime = file.ModTime() + } + } + if oldestFile == nil { + err = os.ErrNotExist + } + return oldestFile.Name() +} + +func GetFileCount(directory string) int { + files, _ := ioutil.ReadDir(directory) + count := len(files) + return count +} diff --git a/wowtools-cli.yml b/wowtools-cli.yml index d497329..ff61d73 100644 --- a/wowtools-cli.yml +++ b/wowtools-cli.yml @@ -4,4 +4,5 @@ elvui_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Interface\\Add elvui_options_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Interface\\AddOns\\ElvUI_OptionsUI\\" addons_dir: "C:\\Program Files (x86)\\World of Warcraft\\_retail_\\Interface\\AddOns" curseforge_exe: "C:\\Program Files (x86)\\Overwolf\\OverwolfLauncher.exe" -curseforge_args: "-launchapp xxxxxxxxxxxxxxxxxxxxxxxxxx -from-startmenu" \ No newline at end of file +curseforge_args: "-launchapp xxxxxxxxxxxxxxxxxxxxxxxxxx -from-startmenu" +retention_rate: 15 \ No newline at end of file