Skip to content

Commit

Permalink
multiple patterns support
Browse files Browse the repository at this point in the history
  • Loading branch information
rostrovsky committed Sep 12, 2024
1 parent bab783c commit 7d81a4a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
40 changes: 20 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ Your analysis should be thorough, insightful, and aimed at enabling AI agents to
`

const (
version = "1.0.4"
version = "1.0.5"
)

var (
rFlag bool
vFlag bool
eFlag string
iFlag string
oFlag string
pFlag string
rFlag bool
vFlag bool
eFlags []string
iFlags []string
oFlag string
pFlag string
)

var rootCmd = &cobra.Command{
Expand All @@ -68,8 +68,8 @@ func init() {
rootCmd.Flags().BoolVarP(&vFlag, "verbose", "v", false, "Enable verbose output")
rootCmd.Flags().StringVarP(&oFlag, "output", "o", "", "Output file path")
rootCmd.Flags().StringVarP(&pFlag, "prompt", "p", "", "Prompt file path or URL")
rootCmd.Flags().StringVarP(&eFlag, "exclude", "e", "", "Regular expression of filename patterns to exclude")
rootCmd.Flags().StringVarP(&iFlag, "include", "i", "", "Regular expression of filename patterns to include")
rootCmd.Flags().StringSliceVarP(&eFlags, "exclude", "e", []string{}, "Regular expressions of filename patterns to exclude")
rootCmd.Flags().StringSliceVarP(&iFlags, "include", "i", []string{}, "Regular expressions of filename patterns to include")

rootCmd.AddCommand(versionCmd)

Expand Down Expand Up @@ -132,26 +132,26 @@ func run(cmd *cobra.Command, args []string) {
prefixToRemove = tempDir
}

var includeRe *regexp.Regexp
var excludeRe *regexp.Regexp
var includeRes []*regexp.Regexp
var excludeRes []*regexp.Regexp

if iFlag != "" {
re, err := regexp.Compile(iFlag)
for _, pattern := range iFlags {
re, err := regexp.Compile(pattern)
if err != nil {
u.LogErrAndExit(err)
u.LogErrAndExit(fmt.Errorf("invalid include pattern '%s': %v", pattern, err))
}
includeRe = re
includeRes = append(includeRes, re)
}

if eFlag != "" {
re, err := regexp.Compile(eFlag)
for _, pattern := range eFlags {
re, err := regexp.Compile(pattern)
if err != nil {
u.LogErrAndExit(err)
u.LogErrAndExit(fmt.Errorf("invalid exclude pattern '%s': %v", pattern, err))
}
excludeRe = re
excludeRes = append(excludeRes, re)
}

err := u.ProcessPath(path, prefixToRemove, includeRe, excludeRe, &sb)
err := u.ProcessPath(path, prefixToRemove, includeRes, excludeRes, &sb)
if err != nil {
u.LogErrAndExit(err)
}
Expand Down
21 changes: 15 additions & 6 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func DetectLanguage(path string) string {
}
}

func ProcessPath(path string, prefixToRemove string, include *regexp.Regexp, exclude *regexp.Regexp, stringBuilder *strings.Builder) error {
func ProcessPath(path string, prefixToRemove string, includes []*regexp.Regexp, excludes []*regexp.Regexp, stringBuilder *strings.Builder) error {
return filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
slog.Debug("Processing", "path", filePath)

Expand All @@ -154,15 +154,24 @@ func ProcessPath(path string, prefixToRemove string, include *regexp.Regexp, exc
}

// skip files that don't match the include pattern
if include != nil && !include.MatchString(trimmedPath) {
slog.Debug("Skipped: doesn't match include pattern", "path", filePath, "trimmedPath", trimmedPath)
matchesInclude := len(includes) == 0 // If no include patterns, match all
for _, include := range includes {
if include.MatchString(trimmedPath) {
matchesInclude = true
break
}
}
if !matchesInclude {
slog.Debug("Skipped: doesn't match any include pattern", "path", filePath, "trimmedPath", trimmedPath)
return nil
}

// skip files that match the exclude pattern
if exclude != nil && exclude.MatchString(trimmedPath) {
slog.Debug("Skipped: matches exclude pattern", "path", filePath, "trimmedPath", trimmedPath)
return nil
for _, exclude := range excludes {
if exclude.MatchString(trimmedPath) {
slog.Debug("Skipped: matches exclude pattern", "path", filePath, "trimmedPath", trimmedPath)
return nil
}
}

// skip binary files
Expand Down

0 comments on commit 7d81a4a

Please sign in to comment.