Skip to content

Commit

Permalink
Feature/folder retention (#17)
Browse files Browse the repository at this point in the history
* Add GetOldestFolder and test

* Add GetFileCount

* Add test for remove file

* Add removeOldElvuiFolder

* Add RemoveOldestWtfZip

* Add retention_rate

* Add RemoveOldestWtfZip to main.go

* Add testing skip for GH actions.
These are manually tested
ldougbmx authored Mar 26, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a062e7e commit 8f7dd0b
Showing 8 changed files with 133 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions cmd/removeOldestFile.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
15 changes: 15 additions & 0 deletions cmd/updateElvUI.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions test/fileDate_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
31 changes: 31 additions & 0 deletions test/removeFile_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
28 changes: 28 additions & 0 deletions utilities/fileHelper.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion wowtools-cli.yml
Original file line number Diff line number Diff line change
@@ -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"
curseforge_args: "-launchapp xxxxxxxxxxxxxxxxxxxxxxxxxx -from-startmenu"
retention_rate: 15

0 comments on commit 8f7dd0b

Please sign in to comment.