aboutsummaryrefslogtreecommitdiff
path: root/internal/scanner/scanner.go
blob: d304605f58eb55259635073d97a0a3b6c06f0e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package scanner

import (
	"context"
	"path/filepath"
)

type Scanner struct {
	config Config
}

func New(config Config) *Scanner {
	clean := make([]string, len(config.Roots))
	for i, root := range config.Roots {
		clean[i] = filepath.Clean(root)
	}

	config.Roots = clean
	return &Scanner{
		config: config,
	}
}

// Scan walks every configured root and returns the discovered files.
//
// The returned slice is not guaranteed to be sorted.
func (s *Scanner) Scan(ctx context.Context) ([]File, error) {
	files := make([]File, 0)
	seen := make(map[string]struct{})

	for _, root := range s.config.Roots {
		discovered, err := s.walk(ctx, root)
		if err != nil {
			return nil, err
		}

		for _, file := range discovered {
			if _, ok := seen[file.Path]; ok {
				continue
			}

			seen[file.Path] = struct{}{}
			files = append(files, file)
		}
	}

	return files, nil
}