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,95 +86,103 @@ 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 configPath, configPathError = game.ConfigPath() var deployedListFile, deployedListCreateError = os.Create(
filepath.Join(cachePath, "deployed.txt"))
if configPathError != nil { if deployedListCreateError != nil {
return configPathError return deployedListCreateError
} }
var backupPath = filepath.Join(cachePath, "backup") defer deployedListFile.Close()
var deployedListPath = filepath.Join(backupPath, "deployed.txt")
if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) { {
defer backupFile.Close() var configPath, configPathError = game.ConfigPath()
var scanner = bufio.NewScanner(backupFile) if configPathError != nil {
return configPathError
for scanner.Scan() {
var
} }
}
var modsPath = filepath.Join(configPath, "mods") var deployedListWriter = bufio.NewWriter(deployedListFile)
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]
if mod.IsEnabled { if mod.IsEnabled {
var modPath = filepath.Join(modsPath, mod.Name) var modPath = filepath.Join(modsPath, mod.Name)
if walkError := filepath.WalkDir(modPath, func( if walkError := filepath.WalkDir(modPath, func(
path string, dirEntry fs.DirEntry, dirError error) error { path string, dirEntry fs.DirEntry, dirError error) error {
if dirError != nil { if dirError != nil {
return dirError return dirError
}
var fileMode = dirEntry.Type()
if !(fileMode.IsDir()) {
var localPath, relativeError = filepath.Rel(modPath, path)
if relativeError != nil {
return relativeError
}
var linkPath = filepath.Join(game.Path, localPath)
if pathError := os.MkdirAll(filepath.Dir(linkPath),
fileMode.Perm()); pathError != nil {
return pathError
}
if _, statError := os.Stat(linkPath); !(os.IsNotExist(statError)) {
var sourceFile, sourceOpenError = os.Open(linkPath)
if sourceOpenError != nil {
return sourceOpenError
}
defer sourceFile.Close()
var targetFile, targetOpenError = os.Create(filepath.Join(restorePath, localPath))
if targetOpenError != nil {
return targetOpenError
}
defer targetFile.Close()
var _, copyError = io.Copy(targetFile, sourceFile)
if copyError != nil {
return copyError
}
}
if linkError := os.Link(path, linkPath); linkError != nil {
return linkError
}
if _, writeError := deployedListWriter.WriteString(linkPath); writeError != nil {
return writeError
}
}
return nil
}); walkError != nil {
return walkError
} }
var fileMode = dirEntry.Type()
if !(fileMode.IsDir()) {
var localPath, relativeError = filepath.Rel(modPath, path)
if relativeError != nil {
return relativeError
}
var linkPath = filepath.Join(game.Path, localPath)
if pathError := os.MkdirAll(filepath.Dir(linkPath),
fileMode.Perm()); pathError != nil {
return pathError
}
if _, statError := os.Stat(linkPath); !(os.IsNotExist(statError)) {
var sourceFile, sourceOpenError = os.Open(linkPath)
if sourceOpenError != nil {
return sourceOpenError
}
defer sourceFile.Close()
var targetFile, targetOpenError = os.Create(backupPath)
if targetOpenError != nil {
return targetOpenError
}
defer targetFile.Close()
var _, copyError = io.Copy(targetFile, sourceFile)
if copyError != nil {
return copyError
}
}
if linkError := os.Link(path, linkPath); linkError != nil {
return linkError
}
}
return nil
}); walkError != nil {
return walkError
} }
} }
} }