Skip to content

Commit f84894f

Browse files
authored
Clean up some code and consolidate variables (#1139)
* Clean up some code and consolidate variables Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> * Update pkg/action/diff.go Signed-off-by: Evan Gibler <20933572+egibs@users.noreply.github.com> --------- Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> Signed-off-by: Evan Gibler <20933572+egibs@users.noreply.github.com>
1 parent bb0fab1 commit f84894f

4 files changed

Lines changed: 79 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: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ type ScanResult struct {
3333
// displayPath mimics diff(1) output for relative paths.
3434
func displayPath(base, path string) string {
3535
if filepath.IsAbs(path) {
36-
rel, err := filepath.Rel(base, path)
37-
if err == nil {
36+
if rel, err := filepath.Rel(base, path); err == nil {
3837
return rel
3938
}
4039
}
@@ -43,9 +42,11 @@ func displayPath(base, path string) string {
4342

4443
// relPath returns the cleanest possible relative path between a source path and files within said path.
4544
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
45+
var (
46+
base, rel string
47+
err error
48+
)
49+
4950
switch {
5051
case isArchive:
5152
fromRoot := fr.ArchiveRoot
@@ -136,10 +137,16 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
136137
}
137138

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

142146
fromReport.Files.Range(func(key, value any) bool {
147+
if ctx.Err() != nil {
148+
return false
149+
}
143150
if key == nil || value == nil {
144151
return true
145152
}
@@ -171,29 +178,21 @@ func relFileReport(ctx context.Context, c malcontent.Config, fromPath string, is
171178

172179
// scoreFile returns a boolean to determine how individual files are stored in a diff report.
173180
func scoreFile(fr, tr *malcontent.FileReport) bool {
174-
scoreSrc := false
175-
scoreDest := false
176-
177181
patterns := []string{
178182
`^[\w.-]+\.so$`,
179183
`^.+-.*-r\d+\.spdx\.json$`,
180184
}
181185

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

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-
}
197196
return false
198197
}
199198

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

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

212210
// If diffing images, use their temporary directories as scan paths
213211
// Flip c.OCI to false when finished to block other image code paths
214-
var isImage bool
215-
var err error
212+
var (
213+
err error
214+
isImage bool
215+
)
216+
216217
if c.OCI {
217218
srcPath, err = archive.OCI(ctx, srcPath)
218219
if err != nil {
@@ -222,24 +223,20 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
222223
if err != nil {
223224
return nil, fmt.Errorf("failed to prepare scan path: %w", err)
224225
}
225-
isImage = true
226-
c.OCI = false
226+
isImage, c.OCI = true, false
227227
}
228228

229229
var g errgroup.Group
230230

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

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

237235
g.Go(func() error {
238236
files, base, err := relFileReport(ctx, c, srcPath, isImage)
239237
res := ScanResult{files: files, base: base, err: err}
240238
if isImage {
241-
res.imageURI = c.ScanPaths[0]
242-
res.tmpRoot = srcPath
239+
res.imageURI, res.tmpRoot = c.ScanPaths[0], srcPath
243240
}
244241
srcCh <- res
245242
return err
@@ -249,8 +246,7 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
249246
files, base, err := relFileReport(ctx, c, destPath, isImage)
250247
res := ScanResult{files: files, base: base, err: err}
251248
if isImage {
252-
res.imageURI = c.ScanPaths[1]
253-
res.tmpRoot = destPath
249+
res.imageURI, res.tmpRoot = c.ScanPaths[1], destPath
254250
}
255251
destCh <- res
256252
return err
@@ -293,7 +289,9 @@ func Diff(ctx context.Context, c malcontent.Config, _ *clog.Logger) (*malcontent
293289
// and employ add/delete for files that are not the same
294290
// When scanning two files, do a 1:1 comparison and
295291
// consider the source -> destination as a change rather than an add/delete
296-
if ((srcInfo.IsDir() && destInfo.IsDir()) || (srcIsArchive && destIsArchive)) || isImage {
292+
shouldHandleDir := ((srcInfo.IsDir() && destInfo.IsDir()) || (srcIsArchive && destIsArchive)) || isImage
293+
294+
if shouldHandleDir {
297295
handleDir(ctx, c, srcResult, destResult, d, isImage)
298296
} else {
299297
var srcFile, destFile *malcontent.FileReport
@@ -330,8 +328,7 @@ func handleDir(ctx context.Context, c malcontent.Config, src, dest ScanResult, d
330328
return
331329
}
332330

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

336333
for path, fr := range src.files {
337334
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)