Skip to content

Commit 25f544b

Browse files
authored
refactor: zerolog -> slog, errors.Wrap -> fmt.Error (#49)
* 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
1 parent 4195463 commit 25f544b

35 files changed

+331
-291
lines changed

.golangci.yml

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ linters-settings:
44
- .Errorf(
55
- errors.New(
66
- errors.Unwrap(
7-
- .Wrap(
8-
- .Wrapf(
97
- .WithMessage(
108
- .WithMessagef(
119
- .WithStack(

cli/cache/cache.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"archive/zip"
55
"encoding/base64"
66
"encoding/json"
7+
"errors"
8+
"fmt"
79
"io"
10+
"log/slog"
811
"os"
912
"path/filepath"
1013
"strings"
1114

12-
"github.com/pkg/errors"
1315
"github.com/puzpuzpuz/xsync/v3"
14-
"github.com/rs/zerolog/log"
1516
"github.com/spf13/viper"
1617
)
1718

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

5354
items, err := os.ReadDir(downloadCache)
5455
if err != nil {
55-
return nil, errors.Wrap(err, "failed reading download cache")
56+
return nil, fmt.Errorf("failed reading download cache: %w", err)
5657
}
5758

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

6667
_, err = addFileToCache(item.Name())
6768
if err != nil {
68-
log.Err(err).Str("file", item.Name()).Msg("failed to add file to cache")
69+
slog.Error("failed to add file to cache", slog.String("file", item.Name()), slog.Any("err", err))
6970
}
7071
}
7172
return loadedCache, nil
@@ -74,7 +75,7 @@ func LoadCache() (*xsync.MapOf[string, []File], error) {
7475
func addFileToCache(filename string) (*File, error) {
7576
cacheFile, err := readCacheFile(filename)
7677
if err != nil {
77-
return nil, errors.Wrap(err, "failed to read cache file")
78+
return nil, fmt.Errorf("failed to read cache file: %w", err)
7879
}
7980

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

9596
zipFile, err := os.Open(path)
9697
if err != nil {
97-
return nil, errors.Wrap(err, "failed to open file")
98+
return nil, fmt.Errorf("failed to open file: %w", err)
9899
}
99100
defer zipFile.Close()
100101

101102
size := stat.Size()
102103
reader, err := zip.NewReader(zipFile, size)
103104
if err != nil {
104-
return nil, errors.Wrap(err, "failed to read zip")
105+
return nil, fmt.Errorf("failed to read zip: %w", err)
105106
}
106107

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

118119
upluginReader, err := upluginFile.Open()
119120
if err != nil {
120-
return nil, errors.Wrap(err, "failed to open uplugin file")
121+
return nil, fmt.Errorf("failed to open uplugin file: %w", err)
121122
}
122123

123124
var uplugin UPlugin
124125
data, err := io.ReadAll(upluginReader)
125126
if err != nil {
126-
return nil, errors.Wrap(err, "failed to read uplugin file")
127+
return nil, fmt.Errorf("failed to read uplugin file: %w", err)
127128
}
128129
if err := json.Unmarshal(data, &uplugin); err != nil {
129-
return nil, errors.Wrap(err, "failed to unmarshal uplugin file")
130+
return nil, fmt.Errorf("failed to unmarshal uplugin file: %w", err)
130131
}
131132

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

134135
hash, err := getFileHash(filename)
135136
if err != nil {
136-
return nil, errors.Wrap(err, "failed to get file hash")
137+
return nil, fmt.Errorf("failed to get file hash: %w", err)
137138
}
138139

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

154155
data, err := io.ReadAll(iconReader)
155156
if err != nil {
156-
return nil, errors.Wrap(err, "failed to read icon file")
157+
return nil, fmt.Errorf("failed to read icon file: %w", err)
157158
}
158159
iconData := base64.StdEncoding.EncodeToString(data)
159160
icon = &iconData

cli/cache/download.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package cache
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"net/http"
78
"os"
89
"path/filepath"
910
"sync"
1011

11-
"github.com/pkg/errors"
1212
"github.com/puzpuzpuz/xsync/v3"
1313
"github.com/spf13/viper"
1414

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

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

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

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

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

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

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

@@ -136,16 +136,16 @@ func downloadInternal(cacheKey string, location string, hash string, url string,
136136
}
137137

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

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

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

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

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

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

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

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

195195
return resp.ContentLength, nil

cli/cache/integrity.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package cache
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io"
7+
"log/slog"
68
"os"
79
"path/filepath"
810
"time"
911

10-
"github.com/pkg/errors"
1112
"github.com/puzpuzpuz/xsync/v3"
12-
"github.com/rs/zerolog/log"
1313
"github.com/spf13/viper"
1414

1515
"github.com/satisfactorymodding/ficsit-cli/utils"
@@ -36,7 +36,7 @@ func getFileHash(file string) (string, error) {
3636
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
3737
stat, err := os.Stat(filepath.Join(downloadCache, file))
3838
if err != nil {
39-
return "", errors.Wrap(err, "failed to stat file")
39+
return "", fmt.Errorf("failed to stat file: %w", err)
4040
}
4141
if stat.Size() != cachedHash.Size || stat.ModTime() != cachedHash.Modified {
4242
return cacheFileHash(file)
@@ -48,16 +48,16 @@ func cacheFileHash(file string) (string, error) {
4848
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
4949
stat, err := os.Stat(filepath.Join(downloadCache, file))
5050
if err != nil {
51-
return "", errors.Wrap(err, "failed to stat file")
51+
return "", fmt.Errorf("failed to stat file: %w", err)
5252
}
5353
f, err := os.Open(filepath.Join(downloadCache, file))
5454
if err != nil {
55-
return "", errors.Wrap(err, "failed to open file")
55+
return "", fmt.Errorf("failed to open file: %w", err)
5656
}
5757
defer f.Close()
5858
hash, err := utils.SHA256Data(f)
5959
if err != nil {
60-
return "", errors.Wrap(err, "failed to hash file")
60+
return "", fmt.Errorf("failed to hash file: %w", err)
6161
}
6262
hashCache.Store(file, hashInfo{
6363
Hash: hash,
@@ -76,19 +76,19 @@ func loadHashCache() {
7676
}
7777
f, err := os.Open(cacheFile)
7878
if err != nil {
79-
log.Warn().Err(err).Msg("failed to open hash cache, recreating")
79+
slog.Warn("failed to open hash cache, recreating", slog.Any("err", err))
8080
return
8181
}
8282
defer f.Close()
8383

8484
hashCacheJSON, err := io.ReadAll(f)
8585
if err != nil {
86-
log.Warn().Err(err).Msg("failed to read hash cache, recreating")
86+
slog.Warn("failed to read hash cache, recreating", slog.Any("err", err))
8787
return
8888
}
8989

9090
if err := json.Unmarshal(hashCacheJSON, &hashCache); err != nil {
91-
log.Warn().Err(err).Msg("failed to unmarshal hash cache, recreating")
91+
slog.Warn("failed to unmarshal hash cache, recreating", slog.Any("err", err))
9292
return
9393
}
9494
}
@@ -102,12 +102,12 @@ func saveHashCache() {
102102
})
103103
hashCacheJSON, err := json.Marshal(plainCache)
104104
if err != nil {
105-
log.Warn().Err(err).Msg("failed to marshal hash cache")
105+
slog.Warn("failed to marshal hash cache", slog.Any("err", err))
106106
return
107107
}
108108

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

0 commit comments

Comments
 (0)