From c5a68af5e6dfe6956060c04a57d503c41613edd5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 13 Apr 2025 21:29:18 +0200 Subject: [PATCH] PROPERLY implement doublestar --- utils/modifycommand.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/utils/modifycommand.go b/utils/modifycommand.go index d773fd6..6c88c52 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -57,6 +57,24 @@ func Matches(path string, glob string) (bool, error) { return matches, nil } +func SplitPattern(pattern string) (string, string) { + static, pattern := doublestar.SplitPattern(pattern) + + cwd, err := os.Getwd() + if err != nil { + return "", "" + } + if static == "" { + static = cwd + } + if !filepath.IsAbs(static) { + static = filepath.Join(cwd, static) + static = filepath.Clean(static) + } + static = strings.ReplaceAll(static, "\\", "/") + return static, pattern +} + type FileCommandAssociation struct { File string IsolateCommands []ModifyCommand @@ -75,7 +93,10 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s } for _, command := range commands { for _, glob := range command.Files { - matches, err := Matches(file, glob) + static, pattern := SplitPattern(glob) + file = strings.ReplaceAll(file, "\\", "/") + file = strings.Replace(file, static+`/`, "", 1) + matches, err := Matches(file, pattern) if err != nil { logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err) continue @@ -132,9 +153,11 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) { logger.Debug("Expanding patterns from directory: %s", cwd) for pattern := range patterns { logger.Trace("Processing pattern: %s", pattern) - matches, _ := doublestar.Glob(os.DirFS(cwd), pattern) + static, pattern := SplitPattern(pattern) + matches, _ := doublestar.Glob(os.DirFS(static), pattern) logger.Debug("Found %d matches for pattern %s", len(matches), pattern) for _, m := range matches { + m = filepath.Join(static, m) info, err := os.Stat(m) if err != nil { logger.Warning("Error getting file info for %s: %v", m, err) @@ -172,18 +195,15 @@ func LoadCommands(args []string) ([]ModifyCommand, error) { } func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) { - cwd, err := os.Getwd() - if err != nil { - return nil, fmt.Errorf("failed to get current working directory: %w", err) - } - + static, pattern := SplitPattern(pattern) commands := []ModifyCommand{} - cookFiles, err := doublestar.Glob(os.DirFS(cwd), pattern) + cookFiles, err := doublestar.Glob(os.DirFS(static), pattern) if err != nil { return nil, fmt.Errorf("failed to glob cook files: %w", err) } for _, cookFile := range cookFiles { + cookFile = filepath.Join(static, cookFile) cookFile = filepath.Clean(cookFile) cookFile = strings.ReplaceAll(cookFile, "\\", "/") logger.Info("Loading commands from cook file: %s", cookFile)