Files
BigChef/utils/modifycommand.go

309 lines
9.3 KiB
Go

package utils
import (
"cook/logger"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar/v4"
"gopkg.in/yaml.v3"
)
type ModifyCommand struct {
Name string `yaml:"name"`
Regex string `yaml:"regex"`
Lua string `yaml:"lua"`
Files []string `yaml:"files"`
Git bool `yaml:"git"`
Reset bool `yaml:"reset"`
LogLevel string `yaml:"loglevel"`
Isolate bool `yaml:"isolate"`
NoDedup bool `yaml:"nodedup"`
}
type CookFile []ModifyCommand
func (c *ModifyCommand) Validate() error {
if c.Regex == "" {
return fmt.Errorf("pattern is required")
}
if c.Lua == "" {
return fmt.Errorf("lua expression is required")
}
if len(c.Files) == 0 {
return fmt.Errorf("at least one file is required")
}
if c.LogLevel == "" {
c.LogLevel = "INFO"
}
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) {
key := fmt.Sprintf("%s:%s", path, glob)
if matches, ok := matchesMemoTable[key]; ok {
logger.Debug("Found match for file %q and glob %q in memo table", path, glob)
return matches, nil
}
matches, err := doublestar.Match(glob, path)
if err != nil {
return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err)
}
matchesMemoTable[key] = matches
return matches, nil
}
type FileCommandAssociation struct {
File string
IsolateCommands []ModifyCommand
Commands []ModifyCommand
}
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string]FileCommandAssociation, error) {
associationCount := 0
fileCommands := make(map[string]FileCommandAssociation)
for _, file := range files {
fileCommands[file] = FileCommandAssociation{
File: file,
IsolateCommands: []ModifyCommand{},
Commands: []ModifyCommand{},
}
for _, command := range commands {
for _, glob := range command.Files {
_, pattern, err := FigureOutGlobRoot(glob)
if err != nil {
logger.Trace("Failed to figure out glob root for %s: %v", glob, err)
continue
}
file = filepath.Clean(file)
file = strings.ReplaceAll(file, "\\", "/")
matches, err := Matches(file, pattern)
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.Regex)
association := fileCommands[file]
if command.Isolate {
association.IsolateCommands = append(association.IsolateCommands, command)
} else {
association.Commands = append(association.Commands, command)
}
fileCommands[file] = association
associationCount++
}
}
}
logger.Debug("Found %d commands for file %q", len(fileCommands[file].Commands), file)
if len(fileCommands[file].Commands) == 0 {
logger.Info("No commands found for file %q", file)
}
if len(fileCommands[file].IsolateCommands) > 0 {
logger.Info("Found %d isolate commands for file %q", len(fileCommands[file].IsolateCommands), 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 {
glob = strings.ReplaceAll(glob, "~", os.Getenv("USERPROFILE"))
glob = strings.ReplaceAll(glob, "\\", "/")
globs[glob] = struct{}{}
}
}
logger.Info("Found %d unique globs", len(globs))
return globs
}
func FigureOutGlobRoot(inputPattern string) (root, pattern string, err error) {
logger.Debug("Starting to figure out glob root for input pattern: %s", inputPattern)
cwd, err := os.Getwd()
if err != nil {
logger.Error("Failed to get current working directory: %v", err)
return "", inputPattern, fmt.Errorf("failed to get current working directory: %w", err)
}
logger.Trace("Current working directory: %s", cwd)
root = inputPattern
if !filepath.IsAbs(inputPattern) {
root = filepath.Join(cwd, inputPattern)
logger.Info("Input pattern is not absolute. Using combined path: %s", root)
}
root = filepath.Clean(root)
logger.Debug("Cleaned root path: %s", root)
// In either case (whatever our root may be), we have to figure out
// Where to start, what our FS will be
// The best place would be the last sure entry
// That is to say the final directory that is not a wildcard
finalroot := ""
// TODO: This will probably explode on linux because oooooooooo we have to be clever oooooooooo / on linux \\ on windows ooooooooooo
parts := strings.Split(root, "\\")
lastIndex := len(parts) - 1
logger.Debug("Split root into parts: %v", parts)
// In the case our pattern ends with a file (and many of them do)
// Look for only the folders, we cannot mount a file as a FS
// In any case we have to match files so they have to be the last part
for i := 0; i < len(parts)-1; i++ {
part := parts[i]
logger.Trace("Processing part: %s", part)
if part == "*" || part == "**" || part == "?" || part == "[" {
lastIndex = i
logger.Debug("Found wildcard part: %s, updating lastIndex to: %d", part, lastIndex)
break
}
// We can't use join here because it joins C: and Users as C:Users
// Instead of C:/Users/
// God damn it
if finalroot != "" {
finalroot = finalroot + "/" + part
} else {
finalroot = finalroot + part
}
}
finalroot = filepath.Clean(finalroot)
logger.Debug("Final root after processing: %s", finalroot)
// After all this juggling our pattern is whatever is left after the finalroot
// Which is, in "worst" case, only a file
pattern = strings.Join(parts[lastIndex:], "/")
logger.Info("Determined pattern: %s", pattern)
return finalroot, pattern, nil
}
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
var files []string
filesMap := make(map[string]bool)
for pattern := range patterns {
root, pattern, err := FigureOutGlobRoot(pattern)
if err != nil {
return nil, fmt.Errorf("failed to figure out glob root: %w", err)
}
logger.Trace("Processing pattern: %s", pattern)
matches, err := doublestar.Glob(os.DirFS(root), pattern)
if err != nil {
return nil, fmt.Errorf("failed to glob pattern %s: %w", pattern, err)
}
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
for _, m := range matches {
m = filepath.Join(root, m)
m = filepath.Clean(m)
m = strings.ReplaceAll(m, "\\", "/")
info, err := os.Stat(m)
if err != nil {
logger.Warning("Error getting file info for %s: %v", m, err)
continue
}
if !info.IsDir() && !filesMap[m] {
logger.Trace("Adding file to process list: %s", m)
filesMap[m], files = true, append(files, m)
}
}
}
if len(files) > 0 {
logger.Debug("Found %d files to process: %v", len(files), files)
}
return files, nil
}
func LoadCommands(args []string) ([]ModifyCommand, error) {
commands := []ModifyCommand{}
logger.Info("Loading commands from cook files: %s", args)
for _, arg := range args {
newcommands, err := LoadCommandsFromCookFiles(arg)
if err != nil {
return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
}
logger.Info("Successfully loaded %d commands from cook iles", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
}
logger.Info("Loaded %d commands from all cook f", len(commands))
return commands, nil
}
func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
root, pattern, err := FigureOutGlobRoot(pattern)
if err != nil {
return nil, fmt.Errorf("failed to figure out glob root: %w", err)
}
commands := []ModifyCommand{}
cookFiles, err := doublestar.Glob(os.DirFS(root), pattern)
if err != nil {
return nil, fmt.Errorf("failed to glob cook files: %w", err)
}
for _, cookFile := range cookFiles {
cookFile = filepath.Clean(cookFile)
cookFile = strings.ReplaceAll(cookFile, "\\", "/")
logger.Info("Loading commands from cook file: %s", cookFile)
cookFileData, err := os.ReadFile(cookFile)
if err != nil {
return nil, fmt.Errorf("failed to read cook file: %w", err)
}
newcommands, err := LoadCommandsFromCookFile(cookFileData)
if err != nil {
return nil, fmt.Errorf("failed to load commands from cook file: %w", err)
}
commands = append(commands, newcommands...)
}
return commands, nil
}
func LoadCommandsFromCookFile(cookFileData []byte) ([]ModifyCommand, error) {
commands := []ModifyCommand{}
err := yaml.Unmarshal(cookFileData, &commands)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal cook file: %w", err)
}
return commands, nil
}
// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
func CountGlobsBeforeDedup(commands []ModifyCommand) int {
count := 0
for _, cmd := range commands {
count += len(cmd.Files)
}
return count
}
func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
filteredCommands := []ModifyCommand{}
filters := strings.Split(filter, ",")
for _, cmd := range commands {
for _, filter := range filters {
if strings.Contains(cmd.Name, filter) {
filteredCommands = append(filteredCommands, cmd)
}
}
}
return filteredCommands
}