Skip to content

Commit b21f87b

Browse files
committed
Clean up some code and consolidate variables
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 47fa30a commit b21f87b

4 files changed

Lines changed: 80 additions & 81 deletions

File tree

pkg/action/action.go

Lines changed: 0 additions & 4 deletions
This file was deleted.

pkg/action/diff.go

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"regexp"
14+
"slices"
1415
"strings"
1516

1617
"github.com/agext/levenshtein"
@@ -33,8 +34,7 @@ type ScanResult struct {
3334
// displayPath mimics diff(1) output for relative paths.
3435
func displayPath(base, path string) string {
3536
if filepath.IsAbs(path) {
36-
rel, err := filepath.Rel(base, path)
37-
if err == nil {
37+
if rel, err := filepath.Rel(base, path); err == nil {
3838
return rel
3939
}
4040
}
@@ -43,9 +43,11 @@ func displayPath(base, path string) string {
4343

4444
// relPath returns the cleanest possible relative path between a source path and files within said path.
4545
func relPath(from string, fr *malcontent.FileReport, isArchive bool, isImage bool) (string, string, error) {
46-
var base string
47-
var err error
48-
var rel string
46+
var (
47+
base, rel string
48+
err error
49+
)
50+
4951
switch {
5052
case isArchive:
5153
fromRoot := fr.ArchiveRoot
@@ -136,10 +138,16 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
136138
}
137139

138140
fromRelPath := map[string]*malcontent.FileReport{}
139-
var base string
140-
var rangeErr error
141+
142+
var (
143+
base string
144+
rangeErr error
145+
)
141146

142147
fromReport.Files.Range(func(key, value any) bool {
148+
if ctx.Err() != nil {
149+
return false
150+
}
143151
if key == nil || value == nil {
144152
return true
145153
}
@@ -171,29 +179,21 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
171179

172180
// scoreFile returns a boolean to determine how individual files are stored in a diff report.
173181
func scoreFile(fr, tr *malcontent.FileReport) bool {
174-
scoreSrc := false
175-
scoreDest := false
176-
177182
patterns := []string{
178183
`^[\w.-]+\.so$`,
179184
`^.+-.*-r\d+\.spdx\.json$`,
180185
}
181186

182187
for _, pattern := range patterns {
183188
re := regexp.MustCompile(pattern)
184-
if re.MatchString(fr.Path) {
185-
scoreSrc = true
186-
}
187-
if re.MatchString(tr.Path) {
188-
scoreDest = true
189+
190+
// If both files match patterns, reeturn true to indicate that `inferMoves` should be used
191+
// Otherwise, indicate that `handleFile` should be used
192+
if re.MatchString(fr.Path) && re.MatchString(tr.Path) {
193+
return true
189194
}
190195
}
191196

192-
// If both files match patterns, reeturn true to indicate that `inferMoves` should be used
193-
// Otherwise, indicate that `handleFile` should be used
194-
if scoreSrc && scoreDest {
195-
return true
196-
}
197197
return false
198198
}
199199

@@ -206,13 +206,15 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
206206
return nil, fmt.Errorf("diff mode requires 2 paths, you passed in %d path(s)", len(c.ScanPaths))
207207
}
208208

209-
srcPath := c.ScanPaths[0]
210-
destPath := c.ScanPaths[1]
209+
srcPath, destPath := c.ScanPaths[0], c.ScanPaths[1]
211210

212211
// If diffing images, use their temporary directories as scan paths
213212
// Flip c.OCI to false when finished to block other image code paths
214-
var isImage bool
215-
var err error
213+
var (
214+
err error
215+
isImage bool
216+
)
217+
216218
if c.OCI {
217219
srcPath, err = archive.OCI(ctx, srcPath)
218220
if err != nil {
@@ -222,24 +224,20 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
222224
if err != nil {
223225
return nil, fmt.Errorf("failed to prepare scan path: %w", err)
224226
}
225-
isImage = true
226-
c.OCI = false
227+
isImage, c.OCI = true, false
227228
}
228229

229230
var g errgroup.Group
230231

231-
srcCh := make(chan ScanResult, 1)
232-
destCh := make(chan ScanResult, 1)
232+
srcCh, destCh := make(chan ScanResult, 1), make(chan ScanResult, 1)
233233

234-
srcIsArchive := programkind.IsSupportedArchive(srcPath)
235-
destIsArchive := programkind.IsSupportedArchive(destPath)
234+
srcIsArchive, destIsArchive := programkind.IsSupportedArchive(srcPath), programkind.IsSupportedArchive(destPath)
236235

237236
g.Go(func() error {
238237
files, base, err := relFileReport(ctx, c, srcPath, isImage)
239238
res := ScanResult{files: files, base: base, err: err}
240239
if isImage {
241-
res.imageURI = c.ScanPaths[0]
242-
res.tmpRoot = srcPath
240+
res.imageURI, res.tmpRoot = c.ScanPaths[0], srcPath
243241
}
244242
srcCh <- res
245243
return err
@@ -249,8 +247,7 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
249247
files, base, err := relFileReport(ctx, c, destPath, isImage)
250248
res := ScanResult{files: files, base: base, err: err}
251249
if isImage {
252-
res.imageURI = c.ScanPaths[1]
253-
res.tmpRoot = destPath
250+
res.imageURI, res.tmpRoot = c.ScanPaths[1], destPath
254251
}
255252
destCh <- res
256253
return err
@@ -293,7 +290,9 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
293290
// and employ add/delete for files that are not the same
294291
// When scanning two files, do a 1:1 comparison and
295292
// consider the source -> destination as a change rather than an add/delete
296-
if ((srcInfo.IsDir() && destInfo.IsDir()) || (srcIsArchive && destIsArchive)) || isImage {
293+
shouldHandleDir := ((srcInfo.IsDir() && destInfo.IsDir()) || (srcIsArchive && destIsArchive)) || isImage
294+
295+
if shouldHandleDir {
297296
handleDir(ctx, c, srcResult, destResult, d, isImage)
298297
} else {
299298
var srcFile, destFile *malcontent.FileReport
@@ -330,8 +329,7 @@ func handleDir(ctx context.Context, c malcontent.Config, src, dest ScanResult, d
330329
return
331330
}
332331

333-
srcFiles := make(map[string]*malcontent.FileReport)
334-
destFiles := make(map[string]*malcontent.FileReport)
332+
srcFiles, destFiles := make(map[string]*malcontent.FileReport), make(map[string]*malcontent.FileReport)
335333

336334
for path, fr := range src.files {
337335
base := filepath.Base(path)

pkg/action/scan.go

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,15 @@ func setupMatchHandler(ctx context.Context, matchChan chan matchResult, c malcon
502502

503503
go func() {
504504
select {
505+
case <-ctx.Done():
506+
return
505507
case match := <-matchChan:
506508
if match.fr != nil && c.Renderer != nil && match.fr.RiskScore >= c.MinFileRisk {
507509
if err := c.Renderer.File(ctx, match.fr); err != nil {
508510
logger.Errorf("render error: %v", err)
509511
}
510512
}
511513
cancel()
512-
case <-ctx.Done():
513-
return
514514
}
515515
}()
516516
}
@@ -545,6 +545,10 @@ func handleArchiveFile(ctx context.Context, path string, c malcontent.Config, r
545545
}
546546
}
547547

548+
if frs == nil {
549+
return nil
550+
}
551+
548552
if !c.OCI && (c.ExitFirstHit || c.ExitFirstMiss) {
549553
match, err := exitIfHitOrMiss(frs, path, c.ExitFirstHit, c.ExitFirstMiss)
550554
if err != nil {
@@ -555,31 +559,30 @@ func handleArchiveFile(ctx context.Context, path string, c malcontent.Config, r
555559
}
556560
}
557561

558-
//nolint:nestif // ignore complexity of 14
559-
if frs != nil {
560-
frs.Range(func(key, value any) bool {
561-
if ctx.Err() != nil {
562-
return false
563-
}
564-
if key == nil || value == nil {
565-
return true
566-
}
567-
if k, ok := key.(string); ok {
568-
if fr, ok := value.(*malcontent.FileReport); ok {
569-
if len(c.TrimPrefixes) > 0 {
570-
k = report.TrimPrefixes(k, c.TrimPrefixes)
571-
}
572-
r.Files.Store(k, fr)
573-
if c.Renderer != nil && r.Diff == nil && fr.RiskScore >= c.MinFileRisk {
574-
if err := c.Renderer.File(ctx, fr); err != nil {
575-
logger.Errorf("render error: %v", err)
576-
}
562+
//nolint:nestif // ignore complexity of 13
563+
frs.Range(func(key, value any) bool {
564+
if ctx.Err() != nil {
565+
return false
566+
}
567+
if key == nil || value == nil {
568+
return true
569+
}
570+
if k, ok := key.(string); ok {
571+
if fr, ok := value.(*malcontent.FileReport); ok {
572+
if len(c.TrimPrefixes) > 0 {
573+
k = report.TrimPrefixes(k, c.TrimPrefixes)
574+
}
575+
r.Files.Store(k, fr)
576+
if c.Renderer != nil && r.Diff == nil && fr.RiskScore >= c.MinFileRisk {
577+
if err := c.Renderer.File(ctx, fr); err != nil {
578+
logger.Errorf("render error: %v", err)
577579
}
578580
}
579581
}
580-
return true
581-
})
582-
}
582+
}
583+
return true
584+
})
585+
583586
return nil
584587
}
585588

@@ -783,23 +786,25 @@ func Scan(ctx context.Context, c malcontent.Config) (*malcontent.Report, error)
783786
if err != nil && !interactive(c) {
784787
return r, err
785788
}
789+
if r == nil {
790+
return nil, nil
791+
}
786792

787-
if r != nil {
788-
r.Files.Range(func(key, value any) bool {
789-
if scanCtx.Err() != nil {
790-
return false
791-
}
792-
if key == nil || value == nil {
793-
return true
794-
}
795-
if fr, ok := value.(*malcontent.FileReport); ok {
796-
if fr.RiskScore < c.MinFileRisk {
797-
r.Files.Delete(key)
798-
}
799-
}
793+
r.Files.Range(func(key, value any) bool {
794+
if scanCtx.Err() != nil {
795+
return false
796+
}
797+
if key == nil || value == nil {
800798
return true
801-
})
802-
}
799+
}
800+
if fr, ok := value.(*malcontent.FileReport); ok {
801+
if fr.RiskScore < c.MinFileRisk {
802+
r.Files.Delete(key)
803+
}
804+
}
805+
return true
806+
})
807+
803808
if scanCtx.Err() == nil && c.Stats && c.Renderer.Name() != "JSON" && c.Renderer.Name() != "YAML" {
804809
err = render.Statistics(&c, r)
805810
if err != nil {

pkg/refresh/refresh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type TestData struct {
3838
func discoverTestData(rc Config) (map[string]string, error) {
3939
testFiles := make(map[string]string)
4040

41-
err := filepath.Walk(rc.TestDataPath, func(path string, info fs.FileInfo, err error) error {
41+
err := filepath.WalkDir(rc.TestDataPath, func(path string, info os.DirEntry, err error) error {
4242
if err != nil {
4343
return err
4444
}

0 commit comments

Comments
 (0)