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
}
var deployedListPath = filepath.Join(cachePath, "backup", "deployed.txt")
var deployedListPath = filepath.Join(cachePath, "deployed.txt")
if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) {
defer backupFile.Close()
if deployedListFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) {
{
defer deployedListFile.Close()
var scanner = bufio.NewScanner(backupFile)
var deployedListScanner = bufio.NewScanner(deployedListFile)
for scanner.Scan() {
var
for deployedListScanner.Scan() {
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
@ -70,95 +86,103 @@ func (game *Game) ConfigPath() (string, error) {
}
func (game *Game) Deploy() error {
if cleanError := game.Clean(); cleanError != nil {
return cleanError
}
var cachePath, cachePathError = game.CachePath()
if cachePathError != nil {
return cachePathError
}
var configPath, configPathError = game.ConfigPath()
var deployedListFile, deployedListCreateError = os.Create(
filepath.Join(cachePath, "deployed.txt"))
if configPathError != nil {
return configPathError
if deployedListCreateError != nil {
return deployedListCreateError
}
var backupPath = filepath.Join(cachePath, "backup")
var deployedListPath = filepath.Join(backupPath, "deployed.txt")
defer deployedListFile.Close()
if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) {
defer backupFile.Close()
{
var configPath, configPathError = game.ConfigPath()
var scanner = bufio.NewScanner(backupFile)
for scanner.Scan() {
var
if configPathError != nil {
return configPathError
}
}
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 {
var mod = game.ModOrder[i]
for i := range game.ModOrder {
var mod = game.ModOrder[i]
if mod.IsEnabled {
var modPath = filepath.Join(modsPath, mod.Name)
if mod.IsEnabled {
var modPath = filepath.Join(modsPath, mod.Name)
if walkError := filepath.WalkDir(modPath, func(
path string, dirEntry fs.DirEntry, dirError error) error {
if walkError := filepath.WalkDir(modPath, func(
path string, dirEntry fs.DirEntry, dirError error) error {
if dirError != nil {
return dirError
if dirError != nil {
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
}
}
}