-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (108 loc) · 2.37 KB
/
main.go
File metadata and controls
132 lines (108 loc) · 2.37 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 main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
var csharpExtensions = map[string]bool{
".cs": true,
".csproj": true,
".sln": true,
".vb": true,
".vbproj": true,
".fs": true,
".fsproj": true,
}
// containsProjectFiles checks if a directory contains any C# files
func containsProjectFiles(dir string) bool {
entries, err := os.ReadDir(dir)
if err != nil {
return false
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
ext := filepath.Ext(entry.Name())
if csharpExtensions[ext] {
return true
}
}
return false
}
func filterDuplicates(paths []string) []string {
seen := make(map[string]bool)
var result []string
for _, p := range paths {
if !seen[p] {
seen[p] = true
result = append(result, p)
}
}
return result
}
var skipDirs = map[string]bool{
"bin": true,
"obj": true,
".git": true,
"node_modules": true,
"vendor": true,
".history": true,
"SPlsWork": true,
"AppData": true,
"scoop": true,
"go": true,
}
func shouldSkipDir(name string) bool {
return skipDirs[name]
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: sharpscan <path>")
os.Exit(1)
}
// Resolve the provided path
root, err := filepath.Abs(os.Args[1])
if err != nil {
fmt.Printf("Error resolving path: %v\n", err)
os.Exit(1)
}
var projects []string
// Walk the directory tree
err = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
// Skip directories we don't have permission to access
// instead of stopping the entire scan
return filepath.SkipDir
}
if !d.IsDir() {
return nil
}
// Skip common non-project directories
if shouldSkipDir(d.Name()) {
return filepath.SkipDir
}
// Check if this directory contains C# files
if !containsProjectFiles(path) {
return nil
}
// Add the project directory
projects = append(projects, path)
// Skip descending into subdirectories once we find a project
return filepath.SkipDir
})
if err != nil {
fmt.Printf("Error walking the path: %v\n", err)
os.Exit(1)
}
// Deduplicate projects
result := filterDuplicates(projects)
// Convert to JSON and print
out, err := json.MarshalIndent(result, "", " ")
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
os.Exit(1)
}
fmt.Println(string(out))
}