-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.go
More file actions
132 lines (118 loc) · 3.82 KB
/
Copy pathanalyzer.go
File metadata and controls
132 lines (118 loc) · 3.82 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package analyzer
import (
"path/filepath"
"strings"
"github.com/turenlabs/batou/internal/fpfilter"
"github.com/turenlabs/batou/internal/rules"
)
// extToLanguage maps file extensions to Language constants.
var extToLanguage = map[string]rules.Language{
".go": rules.LangGo,
".py": rules.LangPython,
".pyw": rules.LangPython,
".js": rules.LangJavaScript,
".jsx": rules.LangJavaScript,
".mjs": rules.LangJavaScript,
".cjs": rules.LangJavaScript,
".ts": rules.LangTypeScript,
".tsx": rules.LangTypeScript,
".mts": rules.LangTypeScript,
".java": rules.LangJava,
".rb": rules.LangRuby,
".erb": rules.LangRuby,
".php": rules.LangPHP,
".kt": rules.LangKotlin,
".kts": rules.LangKotlin,
".groovy": rules.LangGroovy,
".gvy": rules.LangGroovy,
".gy": rules.LangGroovy,
".gsh": rules.LangGroovy,
".gsp": rules.LangGroovy,
"Jenkinsfile": rules.LangGroovy,
".cs": rules.LangCSharp,
".csx": rules.LangCSharp,
".swift": rules.LangSwift,
".rs": rules.LangRust,
".c": rules.LangC,
".h": rules.LangC,
".cpp": rules.LangCPP,
".cc": rules.LangCPP,
".cxx": rules.LangCPP,
".c++": rules.LangCPP,
".hpp": rules.LangCPP,
".hh": rules.LangCPP,
".hxx": rules.LangCPP,
".h++": rules.LangCPP,
".pl": rules.LangPerl,
".pm": rules.LangPerl,
".cgi": rules.LangPerl,
".lua": rules.LangLua,
".zig": rules.LangZig,
".sh": rules.LangShell,
".bash": rules.LangShell,
".zsh": rules.LangShell,
".sql": rules.LangSQL,
".yaml": rules.LangYAML,
".yml": rules.LangYAML,
".json": rules.LangJSON,
".tf": rules.LangTerraform,
".tfvars": rules.LangTerraform,
"Dockerfile": rules.LangDocker,
".dockerfile": rules.LangDocker,
}
// DetectLanguage determines the programming language from a file path.
func DetectLanguage(filePath string) rules.Language {
base := filepath.Base(filePath)
// Check full filename first (e.g., "Dockerfile")
if lang, ok := extToLanguage[base]; ok {
return lang
}
// Check by extension
ext := strings.ToLower(filepath.Ext(filePath))
if lang, ok := extToLanguage[ext]; ok {
return lang
}
return rules.LangAny
}
// IsScannable returns true if the file should be scanned.
// Skips binary files, images, fonts, and generated/vendored files
// (based on path patterns only; content-based generated detection
// happens later in the scanner when content is available).
func IsScannable(filePath string) bool {
ext := strings.ToLower(filepath.Ext(filePath))
skipExts := map[string]bool{
".png": true, ".jpg": true, ".jpeg": true, ".gif": true,
".bmp": true, ".ico": true, ".svg": true, ".webp": true,
".mp3": true, ".mp4": true, ".wav": true, ".avi": true,
".zip": true, ".tar": true, ".gz": true, ".bz2": true,
".exe": true, ".dll": true, ".so": true, ".dylib": true,
".woff": true, ".woff2": true, ".ttf": true, ".eot": true,
".pdf": true, ".doc": true, ".docx": true,
".lock": true,
}
if skipExts[ext] {
return false
}
// Path-only generated file check (no content available yet).
if fpfilter.IsGeneratedFile(filePath, "") {
return false
}
return true
}
// ContentLines splits content into numbered lines for rule scanning.
func ContentLines(content string) []string {
return strings.Split(content, "\n")
}
// FindLineNumber returns the 1-based line number for a byte offset in content.
func FindLineNumber(content string, offset int) int {
if offset < 0 || offset >= len(content) {
return 0
}
line := 1
for i := 0; i < offset && i < len(content); i++ {
if content[i] == '\n' {
line++
}
}
return line
}