-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinteractive.go
More file actions
90 lines (78 loc) · 2.06 KB
/
interactive.go
File metadata and controls
90 lines (78 loc) · 2.06 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
package cmd
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
func runInteractiveMode(cmd *cobra.Command, args []string) {
// Parse exclusions from the --exclude flag if provided
var exclusions []string
if excludeDirs != "" {
for _, dir := range strings.Split(excludeDirs, ",") {
trimmed := strings.TrimSpace(dir)
if trimmed != "" {
exclusions = append(exclusions, trimmed)
}
}
}
files, err := scanGoFiles(".", exclusions)
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to scan for Go files: %v", err))
}
if len(files) == 0 {
fmt.Println("No Go files found in the current directory.")
return
}
printFiles(files)
if promptUser("Do you want to run instrumentation on these files?") {
// Pass the detected files as patterns to Instrument
Instrument(".", files...)
} else {
fmt.Println("Aborting.")
}
}
func printFiles(files []string) {
fmt.Println("Detected Go files:")
for _, file := range files {
fmt.Printf(" - %s\n", file)
}
fmt.Println()
}
func scanGoFiles(root string, excludedDirs []string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check exclusion
if info.IsDir() {
base := info.Name()
for _, excluded := range excludedDirs {
// Simple check: if directory name matches excluded name exacty
// Or if path contains it? User asked to "exclude folders like validation-tests".
// Let's do a strict component match to be safe, or just check if it matches the name.
if base == excluded {
return filepath.SkipDir
}
}
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") {
files = append(files, path)
}
return nil
})
return files, err
}
func promptUser(question string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/N]: ", question)
response, err := reader.ReadString('\n')
if err != nil {
return false
}
response = strings.ToLower(strings.TrimSpace(response))
return response == "y" || response == "yes"
}