-
Notifications
You must be signed in to change notification settings - Fork 83
/
fileignore.go
75 lines (59 loc) · 1.7 KB
/
fileignore.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package fileignore
import (
"bytes"
"io/fs"
"os"
"strings"
"github.com/bearer/curio/pkg/commands/process/settings"
"github.com/monochromegane/go-gitignore"
"github.com/rs/zerolog/log"
)
type FileIgnore struct {
ignorer gitignore.IgnoreMatcher
config settings.Config
}
func New(projectPath string, config settings.Config) *FileIgnore {
return &FileIgnore{
ignorer: ignorerFromStrings(config.Scan.SkipPath),
config: config,
}
}
func (fileignore *FileIgnore) Ignore(projectPath string, filePath string, d fs.DirEntry) bool {
relativePath := strings.TrimPrefix(filePath, projectPath)
trimmedPath := strings.TrimPrefix(relativePath, "/")
fileInfo, err := d.Info()
if err != nil {
log.Error().Msgf("fileInfo err: %s %s", projectPath, relativePath)
return true
}
symlink, _ := isSymlink(projectPath + relativePath)
if symlink {
log.Debug().Msgf("skipping symlink: %s %s", projectPath, relativePath)
return true
}
if fileignore.ignorer.Match(trimmedPath, fileInfo.IsDir()) {
log.Error().Msgf("file ignore match err: %s %s", projectPath, relativePath)
return true
}
if !fileInfo.IsDir() {
if fileInfo.Size() > int64(fileignore.config.Worker.FileSizeMaximum) {
log.Debug().Msgf("skipping file due to size: %s %s", projectPath, relativePath)
return true
}
}
return false
}
func isSymlink(path string) (bool, error) {
fileInfo, err := os.Lstat(path)
if err != nil {
return false, err
}
return fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink, nil
}
func ignorerFromStrings(paths []string) gitignore.IgnoreMatcher {
buffer := bytes.NewBuffer([]byte{})
for _, path := range paths {
buffer.Write([]byte(path + "\n"))
}
return gitignore.NewGitIgnoreFromReader(".", buffer)
}