Skip to content

Commit 605324b

Browse files
committed
golangci-lint: enable modernize
This linter is useful to stay up to date on the latest go std library features. It applies more or less the same set of fixes like go fix does. Changes here were created with "golangci-lint run --fix" automatically. Plus some minor manual cleanup to keep the code more readable. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
1 parent 9b4be4c commit 605324b

15 files changed

Lines changed: 80 additions & 117 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ formatters:
1212

1313
linters:
1414
enable:
15+
- modernize
1516
- nilnesserr
1617
- nolintlint
1718
- revive

add.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
636636
}()
637637
}
638638

639-
wg.Add(1)
640-
go func() {
639+
wg.Go(func() {
641640
b.ContentDigester.Start("")
642641
hashCloser := b.ContentDigester.Hash()
643642
hasher := io.Writer(hashCloser)
@@ -661,8 +660,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
661660
}
662661
hashCloser.Close()
663662
pipeReader.Close()
664-
wg.Done()
665-
}()
663+
})
666664
wg.Wait()
667665
if getErr != nil {
668666
getErr = fmt.Errorf("reading %q: %w", src, getErr)
@@ -739,8 +737,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
739737
latestTimestamp = st.ModTime
740738
}
741739
pipeReader, pipeWriter := io.Pipe()
742-
wg.Add(1)
743-
go func() {
740+
wg.Go(func() {
744741
renamedItems := 0
745742
writer := io.WriteCloser(pipeWriter)
746743
if renameTarget != "" {
@@ -796,10 +793,8 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
796793
if renameTarget != "" && renamedItems > 1 {
797794
renameErr = fmt.Errorf("internal error: renamed %d items when we expected to only rename 1", renamedItems)
798795
}
799-
wg.Done()
800-
}()
801-
wg.Add(1)
802-
go func() {
796+
})
797+
wg.Go(func() {
803798
if st.IsDir {
804799
b.ContentDigester.Start("dir")
805800
} else {
@@ -829,8 +824,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
829824
}
830825
hashCloser.Close()
831826
pipeReader.Close()
832-
wg.Done()
833-
}()
827+
})
834828

835829
wg.Wait()
836830
if getErr != nil {

chroot/run_common.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ func RunUsingChroot(spec *specs.Spec, bundlePath, homeDir string, stdin io.Reade
146146
}
147147

148148
logrus.Debugf("Running %#v in %#v", cmd.Cmd, cmd)
149-
confwg.Add(1)
150-
go func() {
149+
confwg.Go(func() {
151150
_, conferr = io.Copy(pwriter, bytes.NewReader(config))
152151
pwriter.Close()
153-
confwg.Done()
154-
}()
152+
})
155153
cmd.ExtraFiles = append([]*os.File{preader}, cmd.ExtraFiles...)
156154
err = cmd.Run()
157155
confwg.Wait()
@@ -541,12 +539,10 @@ func runUsingChroot(spec *specs.Spec, bundlePath string, ctty *os.File, stdin io
541539
}
542540

543541
logrus.Debugf("Running %#v in %#v", cmd.Cmd, cmd)
544-
confwg.Add(1)
545-
go func() {
542+
confwg.Go(func() {
546543
_, conferr = io.Copy(pwriter, bytes.NewReader(config))
547544
pwriter.Close()
548-
confwg.Done()
549-
}()
545+
})
550546
err = cmd.Run()
551547
confwg.Wait()
552548
signal.Stop(interrupted)

copier/copier.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,20 +837,16 @@ func copierWithSubprocess(bulkReader io.Reader, bulkWriter io.Writer, req reques
837837
stdoutRead = nil
838838
var wg sync.WaitGroup
839839
var readError, writeError error
840-
wg.Add(1)
841-
go func() {
840+
wg.Go(func() {
842841
_, writeError = io.Copy(bulkWriter, bulkWriterRead)
843842
bulkWriterRead.Close()
844843
bulkWriterRead = nil
845-
wg.Done()
846-
}()
847-
wg.Add(1)
848-
go func() {
844+
})
845+
wg.Go(func() {
849846
_, readError = io.Copy(bulkReaderWrite, bulkReader)
850847
bulkReaderWrite.Close()
851848
bulkReaderWrite = nil
852-
wg.Done()
853-
}()
849+
})
854850
wg.Wait()
855851
cmdToWaitFor = nil
856852
if err = cmd.Wait(); err != nil {

copier/copier_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,10 @@ func testGetSingle(t *testing.T) {
798798
pipeReader, pipeWriter := io.Pipe()
799799
var getErr error
800800
var wg sync.WaitGroup
801-
wg.Add(1)
802-
go func() {
801+
wg.Go(func() {
803802
getErr = Get(root, topdir, getOptions, []string{name}, pipeWriter)
804803
pipeWriter.Close()
805-
wg.Done()
806-
}()
804+
})
807805
tr := tar.NewReader(pipeReader)
808806
hdr, err := tr.Next()
809807
for err == nil {
@@ -823,12 +821,10 @@ func testGetSingle(t *testing.T) {
823821
getOptions.StripSetgidBit = stripSetgidBit
824822
getOptions.StripStickyBit = stripStickyBit
825823
pipeReader, pipeWriter := io.Pipe()
826-
wg.Add(1)
827-
go func() {
824+
wg.Go(func() {
828825
getErr = Get(root, topdir, getOptions, []string{name}, pipeWriter)
829826
pipeWriter.Close()
830-
wg.Done()
831-
}()
827+
})
832828
tr := tar.NewReader(pipeReader)
833829
hdr, err := tr.Next()
834830
for err == nil {
@@ -1605,12 +1601,10 @@ func testGetMultiple(t *testing.T) {
16051601
pipeReader, pipeWriter := io.Pipe()
16061602
var getErr error
16071603
var wg sync.WaitGroup
1608-
wg.Add(1)
1609-
go func() {
1610-
defer wg.Done()
1604+
wg.Go(func() {
16111605
getErr = Get(root, topdir, getOptions, []string{testCase.pattern}, pipeWriter)
16121606
pipeWriter.Close()
1613-
}()
1607+
})
16141608
tr := tar.NewReader(pipeReader)
16151609
hdr, err := tr.Next()
16161610
actualContents := []string{}
@@ -3096,13 +3090,11 @@ func testChmod(t *testing.T) {
30963090

30973091
pipeReader, pipeWriter := io.Pipe()
30983092
var wg sync.WaitGroup
3099-
wg.Add(1)
3100-
go func() {
3093+
wg.Go(func() {
31013094
opts := GetOptions{Chmod: v.chmod, ChmodDirs: v.chmodDirs, ChmodFiles: v.chmodFiles}
31023095
err = Get(testDir, "", opts, []string{name}, pipeWriter)
31033096
pipeWriter.Close()
3104-
wg.Done()
3105-
}()
3097+
})
31063098
tr := tar.NewReader(pipeReader)
31073099
hdr, tarErr := tr.Next()
31083100
for tarErr == nil {

digester.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"hash"
88
"io"
9+
"strings"
910
"sync"
1011
"time"
1112

@@ -100,8 +101,7 @@ func newTarFilterer(writeCloser io.WriteCloser, filter func(hdr *tar.Header) (sk
100101
filterer := &tarFilterer{
101102
pipeWriter: pipeWriter,
102103
}
103-
filterer.wg.Add(1)
104-
go func() {
104+
filterer.wg.Go(func() {
105105
filterer.closedLock.Lock()
106106
closed := filterer.closed
107107
filterer.closedLock.Unlock()
@@ -161,8 +161,7 @@ func newTarFilterer(writeCloser io.WriteCloser, filter func(hdr *tar.Header) (sk
161161
} else {
162162
pipeReader.Close()
163163
}
164-
filterer.wg.Done()
165-
}()
164+
})
166165
return filterer
167166
}
168167

@@ -270,17 +269,17 @@ func (c *CompositeDigester) Digest() (string, digest.Digest) {
270269
case 1:
271270
return c.digesters[0].ContentType(), c.digesters[0].Digest()
272271
default:
273-
content := ""
272+
var content strings.Builder
274273
for i, digester := range c.digesters {
275274
if i > 0 {
276-
content += ","
275+
content.WriteString(",")
277276
}
278277
contentType := digester.ContentType()
279278
if contentType != "" {
280279
contentType += ":"
281280
}
282-
content += contentType + digester.Digest().Encoded()
281+
content.WriteString(contentType + digester.Digest().Encoded())
283282
}
284-
return "multi", digest.Canonical.FromString(content)
283+
return "multi", digest.Canonical.FromString(content.String())
285284
}
286285
}

digester_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ func TestTarFilterer(t *testing.T) {
280280
output := make(map[string]string)
281281
pipeReader, pipeWriter := io.Pipe()
282282
var wg sync.WaitGroup
283-
wg.Add(1)
284-
go func() {
283+
wg.Go(func() {
285284
tr := tar.NewReader(pipeReader)
286285
hdr, err := tr.Next()
287286
for err == nil {
@@ -295,8 +294,7 @@ func TestTarFilterer(t *testing.T) {
295294
}
296295
require.Equal(t, io.EOF, err, "unexpected error ended our tarstream read")
297296
pipeReader.Close()
298-
wg.Done()
299-
}()
297+
})
300298
filterer := newTarFilterer(pipeWriter, test.filter)
301299
_, err := io.Copy(filterer, &buffer)
302300
require.Nil(t, err, "unexpected error copying archive through filter to reader")

imagebuildah/executor.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,32 +600,34 @@ func (b *executor) buildStage(ctx context.Context, cleanupStages map[int]*stageE
600600
// processed like regular steps, and if no modification is done to
601601
// layers, its easier to reuse cached layers.
602602
if len(b.labels) > 0 {
603-
labelLine := "LABEL"
603+
var labelLine strings.Builder
604+
labelLine.WriteString("LABEL")
604605
for _, labelSpec := range b.labels {
605606
key, value, _ := strings.Cut(labelSpec, "=")
606607
// check only for an empty key since docker allows empty values
607608
if key != "" {
608-
labelLine += fmt.Sprintf(" %q=%q", key, value)
609+
fmt.Fprintf(&labelLine, " %q=%q", key, value)
609610
}
610611
}
611-
appendInstructions = slices.Concat(appendInstructions, []string{labelLine})
612+
appendInstructions = slices.Concat(appendInstructions, []string{labelLine.String()})
612613
}
613614
}
614615

615616
// If we were given environment variables to set via the API, add them as instructions
616617
// at the beginning of the stage so that they affect subsequent RUN instructions and
617618
// factor into the image history.
618619
if len(b.envs) > 0 {
619-
envLine := "ENV"
620+
var envLine strings.Builder
621+
envLine.WriteString("ENV")
620622
for _, envSpec := range b.envs {
621623
key, value, hasValue := strings.Cut(envSpec, "=")
622624
if hasValue {
623-
envLine += fmt.Sprintf(" %q=%q", key, value)
625+
fmt.Fprintf(&envLine, " %q=%q", key, value)
624626
} else {
625627
return "", nil, false, fmt.Errorf("BUG: unresolved environment variable: %q", key)
626628
}
627629
}
628-
prependInstructions = slices.Concat([]string{envLine}, prependInstructions)
630+
prependInstructions = slices.Concat([]string{envLine.String()}, prependInstructions)
629631
}
630632

631633
// Create stage labels for all stage images including final stage

imagebuildah/stage_executor.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ func joinExcludePatternWithCopySource(srcNorm, excl string) string {
361361
if srcNorm == "." {
362362
return excl
363363
}
364-
if strings.HasPrefix(excl, "!") {
365-
rest := strings.TrimPrefix(excl, "!")
364+
if rest, ok := strings.CutPrefix(excl, "!"); ok {
366365
if rest == "" {
367366
return excl
368367
}
@@ -840,11 +839,12 @@ func (s *stageExecutor) Run(run imagebuilder.Run, config docker.Config) error {
840839
args = []string{run.Files[0].Data}
841840
}
842841
} else {
843-
full := args[0]
842+
var full strings.Builder
843+
full.WriteString(args[0])
844844
for _, file := range run.Files {
845-
full += file.Data + "\n" + file.Name
845+
full.WriteString(file.Data + "\n" + file.Name)
846846
}
847-
args = []string{full}
847+
args = []string{full.String()}
848848
}
849849
}
850850
stageMountPoints, err := s.runStageMountPoints(slices.Concat(run.Mounts, s.executor.transientRunMounts))
@@ -2045,7 +2045,7 @@ func (s *stageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
20452045
case "RUN":
20462046
shArg := ""
20472047
buildArgs := s.getBuildArgsResolvedForRun()
2048-
appendCheckSum := ""
2048+
var appendCheckSum strings.Builder
20492049
for _, flag := range node.Flags {
20502050
var err error
20512051
mountOptionSource := ""
@@ -2108,7 +2108,7 @@ func (s *stageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
21082108
}
21092109
if mountCheckSum != "" {
21102110
// add a separator to appendCheckSum
2111-
appendCheckSum += ":" + mountCheckSum
2111+
appendCheckSum.WriteString(":" + mountCheckSum)
21122112
}
21132113
}
21142114
if len(node.Original) > 4 {
@@ -2126,7 +2126,7 @@ func (s *stageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
21262126
if buildArgs != "" {
21272127
result = result + "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " "
21282128
}
2129-
result = result + "/bin/sh -c " + shArg + heredoc + appendCheckSum + labelsAndAnnotations
2129+
result = result + "/bin/sh -c " + shArg + heredoc + appendCheckSum.String() + labelsAndAnnotations
21302130
return result, nil
21312131
case "ADD", "COPY":
21322132
destination := node
@@ -2804,9 +2804,9 @@ func (s *stageExecutor) EnsureContainerPathAs(path, user string, mode *os.FileMo
28042804
// flag set differently should be reflected in its result. Some build settings
28052805
// only take affect at the final step, so only note those when they're applied.
28062806
func (s *stageExecutor) buildMetadata(isLastStep bool, isAddOrCopy bool) string {
2807-
unsetLabels := ""
2807+
var unsetLabels strings.Builder
28082808
inheritLabels := ""
2809-
unsetAnnotations := ""
2809+
var unsetAnnotations strings.Builder
28102810
inheritAnnotations := ""
28112811
newAnnotations := ""
28122812
layerMutations := ""
@@ -2817,12 +2817,12 @@ func (s *stageExecutor) buildMetadata(isLastStep bool, isAddOrCopy bool) string
28172817
}
28182818
// If --unsetlabel was used to clear a label, make a note of it.
28192819
for _, label := range s.executor.unsetLabels {
2820-
unsetLabels += "|unsetLabel=" + label
2820+
unsetLabels.WriteString("|unsetLabel=" + label)
28212821
}
28222822
if isLastStep {
28232823
// If --unsetannotation was used to clear an annotation, make a note of it.
28242824
for _, annotation := range s.executor.unsetAnnotations {
2825-
unsetAnnotations += "|unsetAnnotation=" + annotation
2825+
unsetAnnotations.WriteString("|unsetAnnotation=" + annotation)
28262826
}
28272827
// If --inherit-annotation was manually set to false then we cleared the inherited annotations.
28282828
if s.executor.inheritAnnotations == types.OptionalBoolFalse {
@@ -2855,7 +2855,7 @@ func (s *stageExecutor) buildMetadata(isLastStep bool, isAddOrCopy bool) string
28552855
}
28562856

28572857
if isAddOrCopy {
2858-
return unsetLabels + " " + inheritLabels + " " + unsetAnnotations + " " + inheritAnnotations + " " + layerMutations + " " + newAnnotations
2858+
return unsetLabels.String() + " " + inheritLabels + " " + unsetAnnotations.String() + " " + inheritAnnotations + " " + layerMutations + " " + newAnnotations
28592859
}
2860-
return unsetLabels + inheritLabels + unsetAnnotations + inheritAnnotations + layerMutations + newAnnotations
2860+
return unsetLabels.String() + inheritLabels + unsetAnnotations.String() + inheritAnnotations + layerMutations + newAnnotations
28612861
}

internal/mkcw/archive.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ func slop(size int64, slop string) int64 {
552552
if factor == "" {
553553
continue
554554
}
555-
if strings.HasSuffix(factor, "%") {
556-
percentage := strings.TrimSuffix(factor, "%")
555+
if percentage, ok := strings.CutSuffix(factor, "%"); ok {
557556
percent, err := strconv.ParseInt(percentage, 10, 8)
558557
if err != nil {
559558
logrus.Warnf("parsing percentage %q: %v", factor, err)

0 commit comments

Comments
 (0)