Skip to content

Commit

Permalink
refactor: zerolog -> slog, errors.Wrap -> fmt.Error (#49)
Browse files Browse the repository at this point in the history
* refactor: zerolog -> slog, errors.Wrap -> fmt.Error

* chore: lint

* fix: correctly handle errors

* fix: use parsed level

* fix: use parsed level, log json to file
  • Loading branch information
Vilsol authored Dec 16, 2023
1 parent 4195463 commit 25f544b
Show file tree
Hide file tree
Showing 35 changed files with 331 additions and 291 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ linters-settings:
- .Errorf(
- errors.New(
- errors.Unwrap(
- .Wrap(
- .Wrapf(
- .WithMessage(
- .WithMessagef(
- .WithStack(
Expand Down
29 changes: 15 additions & 14 deletions cli/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"archive/zip"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/puzpuzpuz/xsync/v3"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func LoadCache() (*xsync.MapOf[string, []File], error) {

items, err := os.ReadDir(downloadCache)
if err != nil {
return nil, errors.Wrap(err, "failed reading download cache")
return nil, fmt.Errorf("failed reading download cache: %w", err)
}

for _, item := range items {
Expand All @@ -65,7 +66,7 @@ func LoadCache() (*xsync.MapOf[string, []File], error) {

_, err = addFileToCache(item.Name())
if err != nil {
log.Err(err).Str("file", item.Name()).Msg("failed to add file to cache")
slog.Error("failed to add file to cache", slog.String("file", item.Name()), slog.Any("err", err))
}
}
return loadedCache, nil
Expand All @@ -74,7 +75,7 @@ func LoadCache() (*xsync.MapOf[string, []File], error) {
func addFileToCache(filename string) (*File, error) {
cacheFile, err := readCacheFile(filename)
if err != nil {
return nil, errors.Wrap(err, "failed to read cache file")
return nil, fmt.Errorf("failed to read cache file: %w", err)
}

loadedCache.Compute(cacheFile.ModReference, func(oldValue []File, _ bool) ([]File, bool) {
Expand All @@ -89,19 +90,19 @@ func readCacheFile(filename string) (*File, error) {
path := filepath.Join(downloadCache, filename)
stat, err := os.Stat(path)
if err != nil {
return nil, errors.Wrap(err, "failed to stat file")
return nil, fmt.Errorf("failed to stat file: %w", err)
}

zipFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "failed to open file")
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer zipFile.Close()

size := stat.Size()
reader, err := zip.NewReader(zipFile, size)
if err != nil {
return nil, errors.Wrap(err, "failed to read zip")
return nil, fmt.Errorf("failed to read zip: %w", err)
}

var upluginFile *zip.File
Expand All @@ -117,23 +118,23 @@ func readCacheFile(filename string) (*File, error) {

upluginReader, err := upluginFile.Open()
if err != nil {
return nil, errors.Wrap(err, "failed to open uplugin file")
return nil, fmt.Errorf("failed to open uplugin file: %w", err)
}

var uplugin UPlugin
data, err := io.ReadAll(upluginReader)
if err != nil {
return nil, errors.Wrap(err, "failed to read uplugin file")
return nil, fmt.Errorf("failed to read uplugin file: %w", err)
}
if err := json.Unmarshal(data, &uplugin); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal uplugin file")
return nil, fmt.Errorf("failed to unmarshal uplugin file: %w", err)
}

modReference := strings.TrimSuffix(upluginFile.Name, ".uplugin")

hash, err := getFileHash(filename)
if err != nil {
return nil, errors.Wrap(err, "failed to get file hash")
return nil, fmt.Errorf("failed to get file hash: %w", err)
}

var iconFile *zip.File
Expand All @@ -147,13 +148,13 @@ func readCacheFile(filename string) (*File, error) {
if iconFile != nil {
iconReader, err := iconFile.Open()
if err != nil {
return nil, errors.Wrap(err, "failed to open icon file")
return nil, fmt.Errorf("failed to open icon file: %w", err)
}
defer iconReader.Close()

data, err := io.ReadAll(iconReader)
if err != nil {
return nil, errors.Wrap(err, "failed to read icon file")
return nil, fmt.Errorf("failed to read icon file: %w", err)
}
iconData := base64.StdEncoding.EncodeToString(data)
icon = &iconData
Expand Down
26 changes: 13 additions & 13 deletions cli/cache/download.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cache

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sync"

"github.com/pkg/errors"
"github.com/puzpuzpuz/xsync/v3"
"github.com/spf13/viper"

Expand Down Expand Up @@ -42,7 +42,7 @@ func DownloadOrCache(cacheKey string, hash string, url string, updates chan<- ut
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
if err := os.MkdirAll(downloadCache, 0o777); err != nil {
if !os.IsExist(err) {
return nil, 0, errors.Wrap(err, "failed creating download cache")
return nil, 0, fmt.Errorf("failed creating download cache: %w", err)
}
}

Expand All @@ -61,7 +61,7 @@ func DownloadOrCache(cacheKey string, hash string, url string, updates chan<- ut

f, err := os.Open(location)
if err != nil {
return nil, 0, errors.Wrap(err, "failed to open file: "+location)
return nil, 0, fmt.Errorf("failed to open file: %s: %w", location, err)
}

return f, group.size, nil
Expand Down Expand Up @@ -107,7 +107,7 @@ func DownloadOrCache(cacheKey string, hash string, url string, updates chan<- ut

f, err := os.Open(location)
if err != nil {
return nil, 0, errors.Wrap(err, "failed to open file: "+location)
return nil, 0, fmt.Errorf("failed to open file: %s: %w", location, err)
}

return f, size, nil
Expand All @@ -121,13 +121,13 @@ func downloadInternal(cacheKey string, location string, hash string, url string,
if hash != "" {
f, err := os.Open(location)
if err != nil {
return 0, errors.Wrap(err, "failed to open file: "+location)
return 0, fmt.Errorf("failed to open file: %s: %w", location, err)
}
defer f.Close()

existingHash, err = utils.SHA256Data(f)
if err != nil {
return 0, errors.Wrap(err, "could not compute hash for file: "+location)
return 0, fmt.Errorf("could not compute hash for file: %s: %w", location, err)
}
}

Expand All @@ -136,16 +136,16 @@ func downloadInternal(cacheKey string, location string, hash string, url string,
}

if err := os.Remove(location); err != nil {
return 0, errors.Wrap(err, "failed to delete file: "+location)
return 0, fmt.Errorf("failed to delete file: %s: %w", location, err)
}
} else if !os.IsNotExist(err) {
return 0, errors.Wrap(err, "failed to stat file: "+location)
return 0, fmt.Errorf("failed to stat file: %s: %w", location, err)
}

if updates != nil {
headResp, err := http.Head(url)
if err != nil {
return 0, errors.Wrap(err, "failed to head: "+url)
return 0, fmt.Errorf("failed to head: %s: %w", url, err)
}
defer headResp.Body.Close()
updates <- utils.GenericProgress{Total: headResp.ContentLength}
Expand All @@ -158,13 +158,13 @@ func downloadInternal(cacheKey string, location string, hash string, url string,

out, err := os.Create(location)
if err != nil {
return 0, errors.Wrap(err, "failed creating file at: "+location)
return 0, fmt.Errorf("failed creating file at: %s: %w", location, err)
}
defer out.Close()

resp, err := http.Get(url)
if err != nil {
return 0, errors.Wrap(err, "failed to fetch: "+url)
return 0, fmt.Errorf("failed to fetch: %s: %w", url, err)
}
defer resp.Body.Close()

Expand All @@ -180,7 +180,7 @@ func downloadInternal(cacheKey string, location string, hash string, url string,

_, err = io.Copy(out, progresser)
if err != nil {
return 0, errors.Wrap(err, "failed writing file to disk")
return 0, fmt.Errorf("failed writing file to disk: %w", err)
}

if updates != nil {
Expand All @@ -189,7 +189,7 @@ func downloadInternal(cacheKey string, location string, hash string, url string,

_, err = addFileToCache(cacheKey)
if err != nil {
return 0, errors.Wrap(err, "failed to add file to cache")
return 0, fmt.Errorf("failed to add file to cache: %w", err)
}

return resp.ContentLength, nil
Expand Down
22 changes: 11 additions & 11 deletions cli/cache/integrity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package cache

import (
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"
"github.com/puzpuzpuz/xsync/v3"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

"github.com/satisfactorymodding/ficsit-cli/utils"
Expand All @@ -36,7 +36,7 @@ func getFileHash(file string) (string, error) {
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
stat, err := os.Stat(filepath.Join(downloadCache, file))
if err != nil {
return "", errors.Wrap(err, "failed to stat file")
return "", fmt.Errorf("failed to stat file: %w", err)
}
if stat.Size() != cachedHash.Size || stat.ModTime() != cachedHash.Modified {
return cacheFileHash(file)
Expand All @@ -48,16 +48,16 @@ func cacheFileHash(file string) (string, error) {
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
stat, err := os.Stat(filepath.Join(downloadCache, file))
if err != nil {
return "", errors.Wrap(err, "failed to stat file")
return "", fmt.Errorf("failed to stat file: %w", err)
}
f, err := os.Open(filepath.Join(downloadCache, file))
if err != nil {
return "", errors.Wrap(err, "failed to open file")
return "", fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
hash, err := utils.SHA256Data(f)
if err != nil {
return "", errors.Wrap(err, "failed to hash file")
return "", fmt.Errorf("failed to hash file: %w", err)
}
hashCache.Store(file, hashInfo{
Hash: hash,
Expand All @@ -76,19 +76,19 @@ func loadHashCache() {
}
f, err := os.Open(cacheFile)
if err != nil {
log.Warn().Err(err).Msg("failed to open hash cache, recreating")
slog.Warn("failed to open hash cache, recreating", slog.Any("err", err))
return
}
defer f.Close()

hashCacheJSON, err := io.ReadAll(f)
if err != nil {
log.Warn().Err(err).Msg("failed to read hash cache, recreating")
slog.Warn("failed to read hash cache, recreating", slog.Any("err", err))
return
}

if err := json.Unmarshal(hashCacheJSON, &hashCache); err != nil {
log.Warn().Err(err).Msg("failed to unmarshal hash cache, recreating")
slog.Warn("failed to unmarshal hash cache, recreating", slog.Any("err", err))
return
}
}
Expand All @@ -102,12 +102,12 @@ func saveHashCache() {
})
hashCacheJSON, err := json.Marshal(plainCache)
if err != nil {
log.Warn().Err(err).Msg("failed to marshal hash cache")
slog.Warn("failed to marshal hash cache", slog.Any("err", err))
return
}

if err := os.WriteFile(cacheFile, hashCacheJSON, 0o755); err != nil {
log.Warn().Err(err).Msg("failed to write hash cache")
slog.Warn("failed to write hash cache", slog.Any("err", err))
return
}
}
Loading

0 comments on commit 25f544b

Please sign in to comment.