Add clean function to manager

This commit is contained in:
kayomn 2022-12-06 09:33:48 +00:00
parent 56d80c4883
commit c55ce0562c
1 changed files with 101 additions and 77 deletions

View File

@ -38,16 +38,32 @@ func (game *Game) Clean() error {
return cachePathError return cachePathError
} }
var deployedListPath = filepath.Join(cachePath, "backup", "deployed.txt") var deployedListPath = filepath.Join(cachePath, "deployed.txt")
if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) { if deployedListFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) {
defer backupFile.Close() {
defer deployedListFile.Close()
var scanner = bufio.NewScanner(backupFile) var deployedListScanner = bufio.NewScanner(deployedListFile)
for scanner.Scan() { for deployedListScanner.Scan() {
var var deployedPath = filepath.Join(game.Path, deployedListScanner.Text())
if removeError := os.Remove(deployedPath); removeError != nil {
return removeError
} }
var deployedDirPath = filepath.Dir(deployedPath)
if remainingDirEntries, readDirError := os.ReadDir(deployedDirPath); (readDirError == nil) && (len(remainingDirEntries) == 0) {
if removeError := os.Remove(deployedDirPath); removeError != nil {
return removeError
}
}
}
}
os.Truncate(deployedListPath, 0)
} }
return nil return nil
@ -70,32 +86,35 @@ func (game *Game) ConfigPath() (string, error) {
} }
func (game *Game) Deploy() error { func (game *Game) Deploy() error {
if cleanError := game.Clean(); cleanError != nil {
return cleanError
}
var cachePath, cachePathError = game.CachePath() var cachePath, cachePathError = game.CachePath()
if cachePathError != nil { if cachePathError != nil {
return cachePathError return cachePathError
} }
var deployedListFile, deployedListCreateError = os.Create(
filepath.Join(cachePath, "deployed.txt"))
if deployedListCreateError != nil {
return deployedListCreateError
}
defer deployedListFile.Close()
{
var configPath, configPathError = game.ConfigPath() var configPath, configPathError = game.ConfigPath()
if configPathError != nil { if configPathError != nil {
return configPathError return configPathError
} }
var backupPath = filepath.Join(cachePath, "backup") var deployedListWriter = bufio.NewWriter(deployedListFile)
var deployedListPath = filepath.Join(backupPath, "deployed.txt")
if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) {
defer backupFile.Close()
var scanner = bufio.NewScanner(backupFile)
for scanner.Scan() {
var
}
}
var modsPath = filepath.Join(configPath, "mods") var modsPath = filepath.Join(configPath, "mods")
var restorePath = filepath.Join(cachePath, "restore")
for i := range game.ModOrder { for i := range game.ModOrder {
var mod = game.ModOrder[i] var mod = game.ModOrder[i]
@ -136,7 +155,7 @@ func (game *Game) Deploy() error {
defer sourceFile.Close() defer sourceFile.Close()
var targetFile, targetOpenError = os.Create(backupPath) var targetFile, targetOpenError = os.Create(filepath.Join(restorePath, localPath))
if targetOpenError != nil { if targetOpenError != nil {
return targetOpenError return targetOpenError
@ -154,6 +173,10 @@ func (game *Game) Deploy() error {
if linkError := os.Link(path, linkPath); linkError != nil { if linkError := os.Link(path, linkPath); linkError != nil {
return linkError return linkError
} }
if _, writeError := deployedListWriter.WriteString(linkPath); writeError != nil {
return writeError
}
} }
return nil return nil
@ -162,6 +185,7 @@ func (game *Game) Deploy() error {
} }
} }
} }
}
return nil return nil
} }