fix logging

This commit is contained in:
2025-07-24 06:41:58 -07:00
parent 7f454dc632
commit 4d47056e5b
2 changed files with 45 additions and 25 deletions

View File

@@ -49,8 +49,8 @@ func main() {
log.Fatalf("WowDir in %s is still the default setting, exiting", cfgPath)
}
stats, err := epoch.Update(config.WowDir, forceFlag, config.RemoveUnknownPatches, false)
if err != nil {
stats := epoch.Update(config.WowDir, forceFlag, config.RemoveUnknownPatches, false)
if stats.Error != nil {
log.Fatal(err)
}

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
@@ -14,18 +13,24 @@ import (
"strings"
)
type DownloadStats struct {
Updated int
Current int
Outdated int
type UpdateStats struct {
Updated int
Current int
Outdated int
LogMessages []string
Error error
}
func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) (DownloadStats, error) {
var stats DownloadStats
func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) UpdateStats {
stats := UpdateStats{
LogMessages: make([]string, 0),
Error: nil,
}
manifest, err := GetManifest()
if err != nil {
log.Fatalf("Failed to get manifest: %v\n", err)
stats.Error = fmt.Errorf("Failed to get manifest: %v\n", err)
return stats
}
for _, file := range manifest.Files {
@@ -37,7 +42,8 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) (D
if _, err = os.Stat(localDir); os.IsNotExist(err) {
err = os.MkdirAll(localDir, 0755)
if err != nil {
return stats, fmt.Errorf("failed to create directory %s: %v", localDir, err)
stats.Error = fmt.Errorf("failed to create directory %s: %v", localDir, err)
return stats
}
}
@@ -45,12 +51,13 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) (D
if _, err = os.Stat(localPath); err == nil {
data, err := os.ReadFile(localPath)
if err != nil {
return stats, err
stats.Error = fmt.Errorf("failed to read %s: %v", localPath, err)
return stats
}
hashBytes := md5.Sum(data)
hash := hex.EncodeToString(hashBytes[:])
if hash == file.Hash {
fmt.Printf("File %s is up to date\n", localPath)
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("File %s is up to date", localPath))
stats.Current += 1
continue
} else {
@@ -60,35 +67,48 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) (D
}
if !skipDownload {
fmt.Printf("Updating %s...\n", localPath)
fmt.Printf("Updating %s...", localPath)
outFile, err := os.Create(localPath)
if err != nil {
return stats, err
stats.Error = fmt.Errorf("failed to create file %s: %v", localPath, err)
return stats
}
downloadSuccess := false
for _, url := range []string{file.Urls.Cloudflare, file.Urls.Digitalocean, file.Urls.None} {
resp, err := http.Get(url)
if err != nil {
outFile.Close()
return stats, err
if resp != nil {
resp.Body.Close()
}
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Failed to download %s: %v", url, err))
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
outFile.Close()
return stats, fmt.Errorf("failed to download update from %s, status code: %d", url, resp.StatusCode)
resp.Body.Close()
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("HTTP Status %d", resp.StatusCode))
continue
}
_, err = io.Copy(outFile, resp.Body)
if err != nil {
outFile.Close()
return stats, err
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Failed to write file %s: %v", localPath, err))
resp.Body.Close()
continue
}
resp.Body.Close()
downloadSuccess = true
break
}
outFile.Close()
if !downloadSuccess {
stats.Error = fmt.Errorf("Failed to download updates, see above messages")
return stats
}
stats.Updated += 1
}
}
@@ -117,16 +137,16 @@ func Update(wowdir string, force bool, removeUnknown bool, skipDownload bool) (D
if err != nil {
return err
}
fmt.Println("Removed unknown patch", d.Name())
stats.LogMessages = append(stats.LogMessages, fmt.Sprintf("Removed unknown patch %s", d.Name()))
}
}
return nil
})
if err != nil {
log.Fatalf("failed to delete unknown patches: %s", err)
stats.Error = fmt.Errorf("failed to delete unknown patches: %v", err)
}
}
return stats, nil
return stats
}