use channel for status messages

This commit is contained in:
2025-08-08 09:27:39 -07:00
parent 6abb9ce47a
commit ccd2586c8d

View File

@@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"
)
@@ -17,20 +18,25 @@ type UpdateStats struct {
Updated int
Current int
Outdated int
LogMessages []string
Error error
MessageBuf []string
}
func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) UpdateStats {
stats := UpdateStats{
LogMessages: make([]string, 0),
Error: nil,
MessageBuf: make([]string, 0),
}
msgChan := make(chan string)
done := make(chan bool)
go func() {
manifest, err := GetManifest()
if err != nil {
stats.Error = fmt.Errorf("Failed to get manifest: %v\n", err)
return stats
done <- true
return
}
for _, file := range manifest.Files {
@@ -43,7 +49,8 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) Up
err = os.MkdirAll(localDir, 0755)
if err != nil {
stats.Error = fmt.Errorf("failed to create directory %s: %v", localDir, err)
return stats
done <- true
return
}
}
@@ -52,12 +59,13 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) Up
data, err := os.ReadFile(localPath)
if err != nil {
stats.Error = fmt.Errorf("failed to read %s: %v", localPath, err)
return stats
done <- true
return
}
hashBytes := md5.Sum(data)
hash := hex.EncodeToString(hashBytes[:])
if hash == file.Hash {
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("File %s is up to date", localPath))
msgChan <- fmt.Sprintf("File %s is up to date", localPath)
stats.Current += 1
continue
} else {
@@ -70,7 +78,8 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) Up
outFile, err := os.Create(localPath)
if err != nil {
stats.Error = fmt.Errorf("failed to create file %s: %v", localPath, err)
return stats
done <- true
return
}
downloadSuccess := false
@@ -80,32 +89,33 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) Up
if resp != nil {
resp.Body.Close()
}
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Failed to download %s: %v", url, err))
msgChan <- fmt.Sprintf("Failed to download %s: %v", url, err)
continue
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("HTTP Status %d", resp.StatusCode))
msgChan <- fmt.Sprintf("HTTP Status %d", resp.StatusCode)
continue
}
_, err = io.Copy(outFile, resp.Body)
if err != nil {
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Failed to write file %s: %v", localPath, err))
msgChan <- fmt.Sprintf("Failed to write file %s: %v", localPath, err)
resp.Body.Close()
continue
}
resp.Body.Close()
downloadSuccess = true
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Successfully downloaded %s", localPath))
msgChan <- fmt.Sprintf("Successfully downloaded %s", localPath)
break
}
outFile.Close()
if !downloadSuccess {
stats.Error = fmt.Errorf("Failed to download updates, see above messages")
return stats
done <- true
return
}
stats.Updated += 1
@@ -125,27 +135,33 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) Up
err = filepath.WalkDir(filepath.Join(wowdir, "Data"), func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && patchreg.MatchString(d.Name()) {
del := true
for _, patch := range patches {
if patch == d.Name() {
if slices.Contains(patches, d.Name()) {
del = false
break
}
}
if del {
err = os.Remove(path)
if err != nil {
return err
}
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Removed unknown patch %s", d.Name()))
msgChan <- fmt.Sprintf("Removed unknown patch %s", d.Name())
}
}
return nil
})
if err != nil {
stats.Error = fmt.Errorf("failed to delete unknown patches: %v", err)
}
}
done <- true
}()
for {
select {
case msg := <-msgChan:
fmt.Println(msg)
stats.MessageBuf = append(stats.MessageBuf, msg)
case <-done:
return stats
}
}
}