Refactor more stuff around
This commit is contained in:
119
main.go
119
main.go
@@ -92,37 +92,15 @@ func main() {
|
||||
|
||||
// The plan is:
|
||||
// Load all commands
|
||||
commands := []ModifyCommand{}
|
||||
logger.Info("Loading commands from cook files: %s", *cookfile)
|
||||
newcommands, err := LoadCommandsFromCookFiles(*cookfile)
|
||||
commands, err := LoadCommands(args)
|
||||
if err != nil {
|
||||
logger.Error("Failed to load commands from cook files: %v", err)
|
||||
logger.Error("Failed to load commands: %v", err)
|
||||
flag.Usage()
|
||||
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
|
||||
logger.Info("Aggregating file patterns for %d commands", len(commands))
|
||||
globs := make(map[string]struct{})
|
||||
for _, command := range commands {
|
||||
for _, glob := range command.Files {
|
||||
globs[glob] = struct{}{}
|
||||
}
|
||||
}
|
||||
globs := AggregateGlobs(commands)
|
||||
|
||||
// Resolve all the files for all the 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))
|
||||
|
||||
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.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)
|
||||
associations, err := AssociateFilesWithCommands(files, commands)
|
||||
if err != nil {
|
||||
logger.Error("Failed to associate files with commands: %v", err)
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
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..
|
||||
// Maybe do....
|
||||
// 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) {
|
||||
// Cannot reset without git, right?
|
||||
if *resetFlag {
|
||||
|
Reference in New Issue
Block a user