Refactor more stuff around

This commit is contained in:
2025-03-27 21:58:52 +01:00
parent 8a40f463f7
commit aac29a4074

119
main.go
View File

@@ -92,37 +92,15 @@ func main() {
// The plan is: // The plan is:
// Load all commands // Load all commands
commands := []ModifyCommand{} commands, err := LoadCommands(args)
logger.Info("Loading commands from cook files: %s", *cookfile)
newcommands, err := LoadCommandsFromCookFiles(*cookfile)
if err != nil { if err != nil {
logger.Error("Failed to load commands from cook files: %v", err) logger.Error("Failed to load commands: %v", err)
flag.Usage() flag.Usage()
return return
} }
logger.Info("Successfully loaded %d commands from cook files", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
logger.Info("Loading commands from arguments: %v", args)
newcommands, err = LoadCommandFromArgs(args)
if err != nil {
logger.Error("Failed to load commands from args: %v", err)
flag.Usage()
return
}
logger.Info("Successfully loaded %d commands from args", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
// Then aggregate all the globs and deduplicate them // Then aggregate all the globs and deduplicate them
logger.Info("Aggregating file patterns for %d commands", len(commands)) globs := AggregateGlobs(commands)
globs := make(map[string]struct{})
for _, command := range commands {
for _, glob := range command.Files {
globs[glob] = struct{}{}
}
}
// Resolve all the files for all the globs // Resolve all the files for all the globs
logger.Info("Found %d unique file patterns", len(globs)) logger.Info("Found %d unique file patterns", len(globs))
@@ -134,27 +112,14 @@ func main() {
} }
logger.Info("Found %d files to process", len(files)) logger.Info("Found %d files to process", len(files))
associationCount := 0 associations, err := AssociateFilesWithCommands(files, commands)
fileCommands := make(map[string][]ModifyCommand) if err != nil {
for _, file := range files { logger.Error("Failed to associate files with commands: %v", err)
for _, command := range commands { flag.Usage()
for _, glob := range command.Files { return
// TODO: Maybe memoize this function call
matches, err := doublestar.Match(glob, file)
if err != nil {
logger.Error("Failed to match glob %s with file %s: %v", glob, file, err)
continue
}
if matches {
logger.Debug("Found match for file %q and command %q", file, command.Pattern)
fileCommands[file] = append(fileCommands[file], command)
associationCount++
}
}
}
logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file)
} }
logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
log.Printf("%#v", associations)
// Somehow connect files to commands via globs.. // Somehow connect files to commands via globs..
// Maybe do.... // Maybe do....
// For each file check every glob of every command // For each file check every glob of every command
@@ -260,6 +225,70 @@ func main() {
} }
} }
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
associationCount := 0
fileCommands := make(map[string][]ModifyCommand)
for _, file := range files {
for _, command := range commands {
for _, glob := range command.Files {
// TODO: Maybe memoize this function call
matches, err := doublestar.Match(glob, file)
if err != nil {
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
continue
}
if matches {
logger.Debug("Found match for file %q and command %q", file, command.Pattern)
fileCommands[file] = append(fileCommands[file], command)
associationCount++
}
}
}
logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file)
if len(fileCommands[file]) == 0 {
logger.Info("No commands found for file %q", file)
}
}
logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
return fileCommands, nil
}
func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
logger.Info("Aggregating globs for %d commands", len(commands))
globs := make(map[string]struct{})
for _, command := range commands {
for _, glob := range command.Files {
globs[glob] = struct{}{}
}
}
logger.Info("Found %d unique globs", len(globs))
return globs
}
func LoadCommands(args []string) ([]ModifyCommand, error) {
commands := []ModifyCommand{}
logger.Info("Loading commands from cook files: %s", *cookfile)
newcommands, err := LoadCommandsFromCookFiles(*cookfile)
if err != nil {
return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
}
logger.Info("Successfully loaded %d commands from cook files", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
logger.Info("Loading commands from arguments: %v", args)
newcommands, err = LoadCommandFromArgs(args)
if err != nil {
return nil, fmt.Errorf("failed to load commands from args: %w", err)
}
logger.Info("Successfully loaded %d commands from args", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
return commands, nil
}
func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) { func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
// Cannot reset without git, right? // Cannot reset without git, right?
if *resetFlag { if *resetFlag {