Refactor modify and replace to their own files
This commit is contained in:
13
utils/flags.go
Normal file
13
utils/flags.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"flag"
|
||||
)
|
||||
|
||||
var (
|
||||
GitFlag = flag.Bool("git", false, "Use git to manage files")
|
||||
ResetFlag = flag.Bool("reset", false, "Reset files to their original state")
|
||||
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
||||
Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
|
||||
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
||||
)
|
128
utils/modifycommand.go
Normal file
128
utils/modifycommand.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"modify/logger"
|
||||
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
)
|
||||
|
||||
type ModifyCommand struct {
|
||||
Pattern string `yaml:"pattern"`
|
||||
LuaExpr string `yaml:"lua"`
|
||||
Files []string `yaml:"files"`
|
||||
Git bool `yaml:"git"`
|
||||
Reset bool `yaml:"reset"`
|
||||
LogLevel string `yaml:"loglevel"`
|
||||
}
|
||||
type CookFile []ModifyCommand
|
||||
|
||||
func (c *ModifyCommand) Validate() error {
|
||||
if c.Pattern == "" {
|
||||
return fmt.Errorf("pattern is required")
|
||||
}
|
||||
if c.LuaExpr == "" {
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
*GitFlag = true
|
||||
}
|
||||
|
||||
if len(args) < 3 {
|
||||
return nil, fmt.Errorf("at least %d arguments are required", 3)
|
||||
}
|
||||
|
||||
command := ModifyCommand{
|
||||
Pattern: args[0],
|
||||
LuaExpr: args[1],
|
||||
Files: args[2:],
|
||||
Git: *GitFlag,
|
||||
Reset: *ResetFlag,
|
||||
LogLevel: *LogLevel,
|
||||
}
|
||||
|
||||
if err := command.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid command: %w", err)
|
||||
}
|
||||
|
||||
return []ModifyCommand{command}, nil
|
||||
}
|
||||
|
||||
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
|
||||
return nil, nil
|
||||
}
|
47
utils/replacecommand.go
Normal file
47
utils/replacecommand.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"modify/logger"
|
||||
)
|
||||
|
||||
type ReplaceCommand struct {
|
||||
From int
|
||||
To int
|
||||
With string
|
||||
}
|
||||
|
||||
func ExecuteModifications(modifications []ReplaceCommand, fileData string) (string, int) {
|
||||
var err error
|
||||
executed := 0
|
||||
for _, modification := range modifications {
|
||||
fileData, err = modification.Execute(fileData)
|
||||
if err != nil {
|
||||
logger.Error("Failed to execute modification: %v", err)
|
||||
continue
|
||||
}
|
||||
executed++
|
||||
}
|
||||
logger.Info("Executed %d modifications", executed)
|
||||
return fileData, executed
|
||||
}
|
||||
|
||||
func (m *ReplaceCommand) Execute(fileDataStr string) (string, error) {
|
||||
err := m.Validate(len(fileDataStr))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to validate modification: %v", err)
|
||||
}
|
||||
|
||||
logger.Trace("Replace pos %d-%d with %q", m.From, m.To, m.With)
|
||||
return fileDataStr[:m.From] + m.With + fileDataStr[m.To:], nil
|
||||
}
|
||||
|
||||
func (m *ReplaceCommand) Validate(maxsize int) error {
|
||||
if m.To < m.From {
|
||||
return fmt.Errorf("command to is less than from: %v", m)
|
||||
}
|
||||
if m.From > maxsize || m.To > maxsize {
|
||||
return fmt.Errorf("command from or to is greater than replacement length: %v", m)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user