Skip to content

Commit 0630b01

Browse files
authored
feat: check headers concurrently (#257)
1 parent 02a7f6d commit 0630b01

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

pkg/header/check.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"path/filepath"
2626
"regexp"
27+
"runtime"
2728
"strings"
2829

2930
eyeignore "github.com/apache/skywalking-eyes/pkg/gitignore"
@@ -35,6 +36,7 @@ import (
3536
"github.com/go-git/go-git/v5"
3637
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
3738
"github.com/go-git/go-git/v5/plumbing/object"
39+
"golang.org/x/sync/errgroup"
3840
)
3941

4042
// Check checks the license headers of the specified paths/globs.
@@ -44,10 +46,18 @@ func Check(config *ConfigHeader, result *Result) error {
4446
return err
4547
}
4648

49+
g := new(errgroup.Group)
50+
g.SetLimit(runtime.GOMAXPROCS(0))
51+
4752
for _, file := range fileList {
48-
if err := CheckFile(file, config, result); err != nil {
49-
return err
50-
}
53+
f := file
54+
g.Go(func() error {
55+
return CheckFile(f, config, result)
56+
})
57+
}
58+
59+
if err := g.Wait(); err != nil {
60+
return err
5161
}
5262

5363
return nil

pkg/header/result.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,68 @@ package header
2020
import (
2121
"fmt"
2222
"strings"
23+
"sync"
2324
)
2425

2526
type Result struct {
27+
mu sync.Mutex
2628
Success []string
2729
Failure []string
2830
Ignored []string
2931
Fixed []string
3032
}
3133

3234
func (result *Result) Fail(file string) {
35+
result.mu.Lock()
3336
result.Failure = append(result.Failure, file)
37+
result.mu.Unlock()
3438
}
3539

3640
func (result *Result) Succeed(file string) {
41+
result.mu.Lock()
3742
result.Success = append(result.Success, file)
43+
result.mu.Unlock()
3844
}
3945

4046
func (result *Result) Ignore(file string) {
47+
result.mu.Lock()
4148
result.Ignored = append(result.Ignored, file)
49+
result.mu.Unlock()
4250
}
4351

4452
func (result *Result) Fix(file string) {
53+
result.mu.Lock()
4554
result.Fixed = append(result.Fixed, file)
55+
result.mu.Unlock()
4656
}
4757

4858
func (result *Result) HasFailure() bool {
49-
return len(result.Failure) > 0
59+
result.mu.Lock()
60+
has := len(result.Failure) > 0
61+
result.mu.Unlock()
62+
return has
5063
}
5164

5265
func (result *Result) Error() error {
53-
return fmt.Errorf(
66+
result.mu.Lock()
67+
msg := fmt.Errorf(
5468
"the following files don't have a valid license header: \n%v",
5569
strings.Join(result.Failure, "\n"),
5670
)
71+
result.mu.Unlock()
72+
return msg
5773
}
5874

5975
func (result *Result) String() string {
60-
return fmt.Sprintf(
76+
result.mu.Lock()
77+
s := fmt.Sprintf(
6178
"Totally checked %d files, valid: %d, invalid: %d, ignored: %d, fixed: %d",
6279
len(result.Success)+len(result.Failure)+len(result.Ignored),
6380
len(result.Success),
6481
len(result.Failure),
6582
len(result.Ignored),
6683
len(result.Fixed),
6784
)
85+
result.mu.Unlock()
86+
return s
6887
}

0 commit comments

Comments
 (0)