Skip to content

Commit 52411d5

Browse files
committed
modernize some code
Results of running the modernize command; go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest modernize -fix ./... Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2531a39 commit 52411d5

File tree

35 files changed

+86
-145
lines changed

35 files changed

+86
-145
lines changed

analyzer/analyzer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func Analyze(ctx context.Context, client *containerd.Client, ref string, opts ..
157157

158158
// Create the container and the task
159159
var container containerd.Container
160-
for i := 0; i < 3; i++ {
160+
for range 3 {
161161
id := xid.New().String()
162162
var s runtimespec.Spec
163163
container, err = client.NewContainer(ctx, id,
@@ -221,8 +221,8 @@ func Analyze(ctx context.Context, client *containerd.Client, ref string, opts ..
221221
prePaths := preMonitor.GetPaths()
222222
for _, path := range prePaths {
223223
cleanPath := path
224-
if strings.HasPrefix(path, target) {
225-
cleanPath = strings.TrimPrefix(path, target)
224+
if after, ok := strings.CutPrefix(path, target); ok {
225+
cleanPath = after
226226
}
227227
if err := rc.Record(cleanPath); err != nil {
228228
log.G(ctx).WithError(err).Debugf("failed to record pre-container path %q", cleanPath)

analyzer/recorder/recorder.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func imageRecorderFromManifest(ctx context.Context, cs content.Store, manifestDe
8080
var eg errgroup.Group
8181
filesMap := make([]map[string]struct{}, len(manifest.Layers))
8282
for i, desc := range manifest.Layers {
83-
i, desc := i, desc
8483
filesMap[i] = make(map[string]struct{})
8584

8685
// Create the index from the layer blob.

analyzer/recorder/recorder_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ func TestNodeIndex(t *testing.T) {
217217
ctx := context.Background()
218218
for _, tt := range tests {
219219
for _, prefix := range allowedPrefix {
220-
prefix := prefix
221220
for mediatype, cWrapper := range compressWrappers {
222221
t.Run(tt.name+":"+mediatype+",prefix="+prefix, func(t *testing.T) {
223222
var layers []ocispec.Descriptor

cache/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func NewDirectoryCache(directory string, config DirectoryCacheConfig) (BlobCache
134134
bufPool := config.BufPool
135135
if bufPool == nil {
136136
bufPool = &sync.Pool{
137-
New: func() interface{} {
137+
New: func() any {
138138
return new(bytes.Buffer)
139139
},
140140
}
@@ -146,7 +146,7 @@ func NewDirectoryCache(directory string, config DirectoryCacheConfig) (BlobCache
146146
maxEntry = defaultMaxLRUCacheEntry
147147
}
148148
dataCache = cacheutil.NewLRUCache(maxEntry)
149-
dataCache.OnEvicted = func(key string, value interface{}) {
149+
dataCache.OnEvicted = func(key string, value any) {
150150
value.(*bytes.Buffer).Reset()
151151
bufPool.Put(value)
152152
}
@@ -158,7 +158,7 @@ func NewDirectoryCache(directory string, config DirectoryCacheConfig) (BlobCache
158158
maxEntry = defaultMaxCacheFds
159159
}
160160
fdCache = cacheutil.NewLRUCache(maxEntry)
161-
fdCache.OnEvicted = func(key string, value interface{}) {
161+
fdCache.OnEvicted = func(key string, value any) {
162162
value.(*os.File).Close()
163163
}
164164
}

cmd/containerd-stargz-grpc/db/reader.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (r *reader) init(decompressedR io.Reader, rOpts metadata.Options) (retErr e
221221

222222
// Initialize root node
223223
var ok bool
224-
for i := 0; i < 100; i++ {
224+
for range 100 {
225225
fsID := xid.New().String()
226226
if err := r.initRootNode(fsID); err != nil {
227227
if errors.Is(err, errbolt.ErrBucketExists) {
@@ -930,10 +930,7 @@ func (fr *fileReader) ReadAt(p []byte, off int64) (n int, err error) {
930930
}
931931

932932
compressedBytesRemain := fr.nextOffset - ent.offset
933-
bufSize := int(2 << 20)
934-
if bufSize > int(compressedBytesRemain) {
935-
bufSize = int(compressedBytesRemain)
936-
}
933+
bufSize := min(int(2<<20), int(compressedBytesRemain))
937934

938935
br := bufio.NewReaderSize(io.NewSectionReader(fr.r.sr, ent.offset, compressedBytesRemain), bufSize)
939936
if _, err := br.Peek(bufSize); err != nil {

cmd/containerd-stargz-grpc/ipfs/resolvehandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ func (f *fetcher) Check() error {
7474
}
7575

7676
func (f *fetcher) GenID(off int64, size int64) string {
77-
sum := sha256.Sum256([]byte(fmt.Sprintf("%s-%d-%d", f.cid, off, size)))
77+
sum := sha256.Sum256(fmt.Appendf(nil, "%s-%d-%d", f.cid, off, size))
7878
return fmt.Sprintf("%x", sum)
7979
}

cmd/stargz-store/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626
"io"
2727
golog "log"
28+
"maps"
2829
"math/rand"
2930
"net"
3031
"os"
@@ -250,9 +251,7 @@ func (sk *storeKeychain) add(data []byte) error {
250251
if sk.config == nil {
251252
sk.config = make(map[string]authConfig)
252253
}
253-
for k, c := range conf {
254-
sk.config[k] = c
255-
}
254+
maps.Copy(sk.config, conf)
256255
sk.configMu.Unlock()
257256
return nil
258257
}

estargz/build.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ func Build(tarBlob *io.SectionReader, opt ...Option) (_ *Blob, rErr error) {
238238
var mu sync.Mutex
239239
var eg errgroup.Group
240240
for i, parts := range tarParts {
241-
i, parts := i, parts
242241
// builds verifiable stargz sub-blobs
243242
eg.Go(func() error {
244243
esgzFile, err := layerFiles.TempFile("", "esgzdata")

estargz/build_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,8 @@ func TestSort(t *testing.T) {
354354
}
355355
for _, tt := range tests {
356356
for _, srcCompression := range srcCompressions {
357-
srcCompression := srcCompression
358357
for _, logprefix := range allowedPrefix {
359-
logprefix := logprefix
360358
for _, tarprefix := range allowedPrefix {
361-
tarprefix := tarprefix
362359
t.Run(fmt.Sprintf("%s-logprefix=%q-tarprefix=%q-src=%d", tt.name, logprefix, tarprefix, srcCompression), func(t *testing.T) {
363360
// Sort tar file
364361
var pfiles []string

estargz/estargz_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ func regularFileReader(name string, size int64, chunkSize int64) (*TOCEntry, *Re
101101
var written int64
102102
for written < size {
103103
remain := size - written
104-
cs := chunkSize
105-
if remain < cs {
106-
cs = remain
107-
}
104+
cs := min(remain, chunkSize)
108105
ent.ChunkSize = cs
109106
ent.ChunkOffset = written
110107
chunks = append(chunks, ent)

0 commit comments

Comments
 (0)