package utils import ( "fmt" "os" "path/filepath" "strings" logger "git.site.quack-lab.dev/dave/cylogger" "github.com/bmatcuk/doublestar/v4" "gopkg.in/yaml.v3" ) // modifyCommandLogger is a scoped logger for the utils/modifycommand package. var modifyCommandLogger = logger.Default.WithPrefix("utils/modifycommand") type ModifyCommand struct { Name string `yaml:"name,omitempty"` Regex string `yaml:"regex,omitempty"` Regexes []string `yaml:"regexes,omitempty"` Lua string `yaml:"lua,omitempty"` Files []string `yaml:"files,omitempty"` Reset bool `yaml:"reset,omitempty"` LogLevel string `yaml:"loglevel,omitempty"` Isolate bool `yaml:"isolate,omitempty"` NoDedup bool `yaml:"nodedup,omitempty"` Disabled bool `yaml:"disable,omitempty"` Modifiers map[string]interface{} `yaml:"modifiers,omitempty"` } type CookFile []ModifyCommand func (c *ModifyCommand) Validate() error { validateLogger := modifyCommandLogger.WithPrefix("Validate").WithField("commandName", c.Name) validateLogger.Debug("Validating command") if c.Regex == "" && len(c.Regexes) == 0 { validateLogger.Error("Validation failed: Regex pattern is required") return fmt.Errorf("pattern is required") } if c.Lua == "" { validateLogger.Error("Validation failed: Lua expression is required") return fmt.Errorf("lua expression is required") } if len(c.Files) == 0 { validateLogger.Error("Validation failed: At least one file is required") return fmt.Errorf("at least one file is required") } if c.LogLevel == "" { validateLogger.Debug("LogLevel not specified, defaulting to INFO") c.LogLevel = "INFO" } validateLogger.Debug("Command validated successfully") return nil } // Ehh.. Not much better... Guess this wasn't the big deal var matchesMemoTable map[string]bool = make(map[string]bool) func Matches(path string, glob string) (bool, error) { matchesLogger := modifyCommandLogger.WithPrefix("Matches").WithField("path", path).WithField("glob", glob) matchesLogger.Debug("Checking if path matches glob") key := fmt.Sprintf("%s:%s", path, glob) if matches, ok := matchesMemoTable[key]; ok { matchesLogger.Debug("Found match in memo table: %t", matches) return matches, nil } matches, err := doublestar.Match(glob, path) if err != nil { matchesLogger.Error("Failed to match glob: %v", err) return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err) } matchesMemoTable[key] = matches matchesLogger.Debug("Match result: %t, storing in memo table", matches) return matches, nil } func SplitPattern(pattern string) (string, string) { splitPatternLogger := modifyCommandLogger.WithPrefix("SplitPattern").WithField("pattern", pattern) splitPatternLogger.Debug("Splitting pattern") splitPatternLogger.Trace("Original pattern: %q", pattern) static, pattern := doublestar.SplitPattern(pattern) cwd, err := os.Getwd() if err != nil { splitPatternLogger.Error("Error getting current working directory: %v", err) return "", "" } splitPatternLogger.Trace("Current working directory: %q", cwd) if static == "" { splitPatternLogger.Debug("Static part is empty, defaulting to current working directory") static = cwd } if !filepath.IsAbs(static) { splitPatternLogger.Debug("Static part is not absolute, joining with current working directory") static = filepath.Join(cwd, static) static = filepath.Clean(static) splitPatternLogger.Trace("Static path after joining and cleaning: %q", static) } static = strings.ReplaceAll(static, "\\", "/") splitPatternLogger.Trace("Final static path: %q, Remaining pattern: %q", static, pattern) return static, pattern } type FileCommandAssociation struct { File string IsolateCommands []ModifyCommand Commands []ModifyCommand } func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string]FileCommandAssociation, error) { associateFilesLogger := modifyCommandLogger.WithPrefix("AssociateFilesWithCommands") associateFilesLogger.Debug("Associating files with commands") associateFilesLogger.Trace("Input files: %v", files) associateFilesLogger.Trace("Input commands: %v", commands) associationCount := 0 fileCommands := make(map[string]FileCommandAssociation) for _, file := range files { file = strings.ReplaceAll(file, "\\", "/") associateFilesLogger.Debug("Processing file: %q", file) fileCommands[file] = FileCommandAssociation{ File: file, IsolateCommands: []ModifyCommand{}, Commands: []ModifyCommand{}, } for _, command := range commands { associateFilesLogger.Debug("Checking command %q for file %q", command.Name, file) for _, glob := range command.Files { glob = strings.ReplaceAll(glob, "\\", "/") static, pattern := SplitPattern(glob) associateFilesLogger.Trace("Glob parts for %q → static=%q pattern=%q", glob, static, pattern) // Build absolute path for the current file to compare with static cwd, err := os.Getwd() if err != nil { associateFilesLogger.Warning("Failed to get CWD when matching %q for file %q: %v", glob, file, err) continue } var absFile string if filepath.IsAbs(file) { absFile = filepath.Clean(file) } else { absFile = filepath.Clean(filepath.Join(cwd, file)) } absFile = strings.ReplaceAll(absFile, "\\", "/") associateFilesLogger.Trace("Absolute file path resolved for matching: %q", absFile) // Only match if the file is under the static root if !(strings.HasPrefix(absFile, static+"/") || absFile == static) { associateFilesLogger.Trace("Skipping glob %q for file %q because file is outside static root %q", glob, file, static) continue } patternFile := strings.TrimPrefix(absFile, static+`/`) associateFilesLogger.Trace("Pattern-relative path used for match: %q", patternFile) matches, err := Matches(patternFile, pattern) if err != nil { associateFilesLogger.Warning("Failed to match glob %q with file %q: %v", glob, file, err) continue } if matches { associateFilesLogger.Debug("File %q matches glob %q. Associating with command %q", file, glob, command.Name) association := fileCommands[file] if command.Isolate { associateFilesLogger.Debug("Command %q is an isolate command, adding to isolate list", command.Name) association.IsolateCommands = append(association.IsolateCommands, command) } else { associateFilesLogger.Debug("Command %q is a regular command, adding to regular list", command.Name) association.Commands = append(association.Commands, command) } fileCommands[file] = association associationCount++ } else { associateFilesLogger.Trace("File %q did not match glob %q (pattern=%q, rel=%q)", file, glob, pattern, patternFile) } } } currentFileCommands := fileCommands[file] associateFilesLogger.Debug("Finished processing file %q. Found %d regular commands and %d isolate commands", file, len(currentFileCommands.Commands), len(currentFileCommands.IsolateCommands)) associateFilesLogger.Trace("Commands for file %q: %v", file, currentFileCommands.Commands) associateFilesLogger.Trace("Isolate commands for file %q: %v", file, currentFileCommands.IsolateCommands) } associateFilesLogger.Info("Completed association. Found %d total associations for %d files and %d commands", associationCount, len(files), len(commands)) return fileCommands, nil } func AggregateGlobs(commands []ModifyCommand) map[string]struct{} { aggregateGlobsLogger := modifyCommandLogger.WithPrefix("AggregateGlobs") aggregateGlobsLogger.Debug("Aggregating glob patterns from commands") aggregateGlobsLogger.Trace("Input commands for aggregation: %v", commands) globs := make(map[string]struct{}) for _, command := range commands { aggregateGlobsLogger.Debug("Processing command %q for glob patterns", command.Name) for _, glob := range command.Files { resolvedGlob := strings.Replace(glob, "~", os.Getenv("HOME"), 1) resolvedGlob = strings.ReplaceAll(resolvedGlob, "\\", "/") aggregateGlobsLogger.Trace("Adding glob: %q (resolved to %q)", glob, resolvedGlob) globs[resolvedGlob] = struct{}{} } } aggregateGlobsLogger.Debug("Finished aggregating globs. Found %d unique glob patterns", len(globs)) aggregateGlobsLogger.Trace("Aggregated unique globs: %v", globs) return globs } func ExpandGLobs(patterns map[string]struct{}) ([]string, error) { expandGlobsLogger := modifyCommandLogger.WithPrefix("ExpandGLobs") expandGlobsLogger.Debug("Expanding glob patterns to actual files") expandGlobsLogger.Trace("Input patterns for expansion: %v", patterns) var files []string filesMap := make(map[string]bool) cwd, err := os.Getwd() if err != nil { expandGlobsLogger.Error("Failed to get current working directory: %v", err) return nil, fmt.Errorf("failed to get current working directory: %w", err) } expandGlobsLogger.Debug("Current working directory: %q", cwd) for pattern := range patterns { expandGlobsLogger.Debug("Processing glob pattern: %q", pattern) static, pattern := SplitPattern(pattern) matches, err := doublestar.Glob(os.DirFS(static), pattern) if err != nil { expandGlobsLogger.Warning("Error expanding glob %q in %q: %v", pattern, static, err) continue } expandGlobsLogger.Debug("Found %d matches for pattern %q", len(matches), pattern) expandGlobsLogger.Trace("Raw matches for pattern %q: %v", pattern, matches) for _, m := range matches { m = filepath.Join(static, m) info, err := os.Stat(m) if err != nil { expandGlobsLogger.Warning("Error getting file info for %q: %v", m, err) continue } if !info.IsDir() && !filesMap[m] { expandGlobsLogger.Trace("Adding unique file to list: %q", m) filesMap[m], files = true, append(files, m) } } } if len(files) > 0 { expandGlobsLogger.Debug("Finished expanding globs. Found %d unique files to process", len(files)) expandGlobsLogger.Trace("Unique files to process: %v", files) } else { expandGlobsLogger.Warning("No files found after expanding glob patterns.") } return files, nil } func LoadCommands(args []string) ([]ModifyCommand, error) { loadCommandsLogger := modifyCommandLogger.WithPrefix("LoadCommands") loadCommandsLogger.Debug("Loading commands from arguments (cook files or direct patterns)") loadCommandsLogger.Trace("Input arguments: %v", args) commands := []ModifyCommand{} for _, arg := range args { loadCommandsLogger.Debug("Processing argument for commands: %q", arg) newCommands, err := LoadCommandsFromCookFiles(arg) if err != nil { loadCommandsLogger.Error("Failed to load commands from argument %q: %v", arg, err) return nil, fmt.Errorf("failed to load commands from cook files: %w", err) } loadCommandsLogger.Debug("Successfully loaded %d commands from %q", len(newCommands), arg) for _, cmd := range newCommands { if cmd.Disabled { loadCommandsLogger.Debug("Skipping disabled command: %q", cmd.Name) continue } commands = append(commands, cmd) loadCommandsLogger.Trace("Added command %q. Current total commands: %d", cmd.Name, len(commands)) } } loadCommandsLogger.Info("Finished loading commands. Total %d commands loaded", len(commands)) return commands, nil } func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) { loadCookFilesLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromCookFiles").WithField("pattern", pattern) loadCookFilesLogger.Debug("Loading commands from cook files based on pattern") loadCookFilesLogger.Trace("Input pattern: %q", pattern) static, pattern := SplitPattern(pattern) commands := []ModifyCommand{} cookFiles, err := doublestar.Glob(os.DirFS(static), pattern) if err != nil { loadCookFilesLogger.Error("Failed to glob cook files for pattern %q: %v", pattern, err) return nil, fmt.Errorf("failed to glob cook files: %w", err) } loadCookFilesLogger.Debug("Found %d cook files for pattern %q", len(cookFiles), pattern) loadCookFilesLogger.Trace("Cook files found: %v", cookFiles) for _, cookFile := range cookFiles { cookFile = filepath.Join(static, cookFile) cookFile = filepath.Clean(cookFile) cookFile = strings.ReplaceAll(cookFile, "\\", "/") loadCookFilesLogger.Debug("Loading commands from individual cook file: %q", cookFile) cookFileData, err := os.ReadFile(cookFile) if err != nil { loadCookFilesLogger.Error("Failed to read cook file %q: %v", cookFile, err) return nil, fmt.Errorf("failed to read cook file: %w", err) } loadCookFilesLogger.Trace("Read %d bytes from cook file %q", len(cookFileData), cookFile) newCommands, err := LoadCommandsFromCookFile(cookFileData) if err != nil { loadCookFilesLogger.Error("Failed to load commands from cook file data for %q: %v", cookFile, err) return nil, fmt.Errorf("failed to load commands from cook file: %w", err) } commands = append(commands, newCommands...) loadCookFilesLogger.Debug("Added %d commands from cook file %q. Total commands now: %d", len(newCommands), cookFile, len(commands)) } loadCookFilesLogger.Debug("Finished loading commands from cook files. Total %d commands", len(commands)) return commands, nil } func LoadCommandsFromCookFile(cookFileData []byte) ([]ModifyCommand, error) { loadCommandLogger := modifyCommandLogger.WithPrefix("LoadCommandsFromCookFile") loadCommandLogger.Debug("Unmarshaling commands from cook file data") loadCommandLogger.Trace("Cook file data length: %d", len(cookFileData)) commands := []ModifyCommand{} err := yaml.Unmarshal(cookFileData, &commands) if err != nil { loadCommandLogger.Error("Failed to unmarshal cook file data: %v", err) return nil, fmt.Errorf("failed to unmarshal cook file: %w", err) } loadCommandLogger.Debug("Successfully unmarshaled %d commands", len(commands)) loadCommandLogger.Trace("Unmarshaled commands: %v", commands) return commands, nil } // CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication func CountGlobsBeforeDedup(commands []ModifyCommand) int { countGlobsLogger := modifyCommandLogger.WithPrefix("CountGlobsBeforeDedup") countGlobsLogger.Debug("Counting glob patterns before deduplication") count := 0 for _, cmd := range commands { countGlobsLogger.Trace("Processing command %q, adding %d globs", cmd.Name, len(cmd.Files)) count += len(cmd.Files) } countGlobsLogger.Debug("Total glob patterns before deduplication: %d", count) return count } func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand { filterCommandsLogger := modifyCommandLogger.WithPrefix("FilterCommands").WithField("filter", filter) filterCommandsLogger.Debug("Filtering commands") filterCommandsLogger.Trace("Input commands: %v", commands) filteredCommands := []ModifyCommand{} filters := strings.Split(filter, ",") filterCommandsLogger.Trace("Split filters: %v", filters) for _, cmd := range commands { filterCommandsLogger.Debug("Checking command %q against filters", cmd.Name) for _, f := range filters { if strings.Contains(cmd.Name, f) { filterCommandsLogger.Debug("Command %q matches filter %q, adding to filtered list", cmd.Name, f) filteredCommands = append(filteredCommands, cmd) break // Command matches, no need to check other filters } } } filterCommandsLogger.Debug("Finished filtering commands. Found %d filtered commands", len(filteredCommands)) filterCommandsLogger.Trace("Filtered commands: %v", filteredCommands) return filteredCommands }