Work out file reading and writing
This commit is contained in:
54
main.go
54
main.go
@@ -44,6 +44,11 @@ type ModifyCommand struct {
|
||||
Reset bool `yaml:"reset"`
|
||||
LogLevel string `yaml:"loglevel"`
|
||||
}
|
||||
type ReplaceCommand struct {
|
||||
From int
|
||||
To int
|
||||
With string
|
||||
}
|
||||
type CookFile []ModifyCommand
|
||||
|
||||
func (c *ModifyCommand) Validate() error {
|
||||
@@ -107,25 +112,48 @@ func main() {
|
||||
files, err := ExpandGLobs(globs)
|
||||
if err != nil {
|
||||
logger.Error("Failed to expand file patterns: %v", err)
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
logger.Info("Found %d files to process", len(files))
|
||||
|
||||
associations, err := AssociateFilesWithCommands(files, commands)
|
||||
if err != nil {
|
||||
logger.Error("Failed to associate files with commands: %v", err)
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("%#v", associations)
|
||||
// Somehow connect files to commands via globs..
|
||||
// Maybe do....
|
||||
// For each file check every glob of every command
|
||||
// Maybe memoize this part
|
||||
// That way we know what commands affect what files
|
||||
associations, err := AssociateFilesWithCommands(files, commands)
|
||||
if err != nil {
|
||||
logger.Error("Failed to associate files with commands: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Utilize parallel workers for this
|
||||
// Then for each file run all commands associated with the file
|
||||
for file, commands := range associations {
|
||||
fileData, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
logger.Error("Failed to read file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
||||
|
||||
modifications := []ReplaceCommand{}
|
||||
for _, command := range commands {
|
||||
logger.Info("Processing file %q with command %q", file, command.Pattern)
|
||||
// TODO: Run processor and return modifications
|
||||
}
|
||||
|
||||
for _, modification := range modifications {
|
||||
logger.Info("Modifying file %q from %d to %d with %q", file, modification.From, modification.To, modification.With)
|
||||
// TODO: Execute modifications
|
||||
}
|
||||
|
||||
err = os.WriteFile(file, fileData, 0644)
|
||||
if err != nil {
|
||||
logger.Error("Failed to write file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Aggregate all the modifications and execute them
|
||||
// Taking care of the whole duplicates and shit
|
||||
// This will also relieve processor of some of the file loading
|
||||
@@ -296,8 +324,6 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
||||
}
|
||||
|
||||
if len(args) < 3 {
|
||||
logger.Error("At least %d arguments are required", 3)
|
||||
flag.Usage()
|
||||
return nil, fmt.Errorf("at least %d arguments are required", 3)
|
||||
}
|
||||
|
||||
@@ -311,9 +337,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
||||
}
|
||||
|
||||
if err := command.Validate(); err != nil {
|
||||
logger.Error("Invalid command: %v", err)
|
||||
flag.Usage()
|
||||
return nil, fmt.Errorf("invalid command: %v", err)
|
||||
return nil, fmt.Errorf("invalid command: %w", err)
|
||||
}
|
||||
|
||||
return []ModifyCommand{command}, nil
|
||||
|
@@ -31,15 +31,6 @@ type Processor interface {
|
||||
FromLua(L *lua.LState) (interface{}, error)
|
||||
}
|
||||
|
||||
// ModificationRecord tracks a single value modification
|
||||
type ModificationRecord struct {
|
||||
File string
|
||||
OldValue string
|
||||
NewValue string
|
||||
Operation string
|
||||
Context string
|
||||
}
|
||||
|
||||
func NewLuaState() (*lua.LState, error) {
|
||||
L := lua.NewState()
|
||||
// defer L.Close()
|
||||
|
@@ -90,11 +90,6 @@ type CaptureGroup struct {
|
||||
Updated string
|
||||
Range [2]int
|
||||
}
|
||||
type ReplaceCommand struct {
|
||||
From int
|
||||
To int
|
||||
With string
|
||||
}
|
||||
|
||||
// ProcessContent applies regex replacement with Lua processing
|
||||
func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {
|
||||
|
Reference in New Issue
Block a user