From 5929c277319008528d17f1adc1f5c1d05c6a64a3 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 22 Mar 2025 01:19:29 +0100 Subject: [PATCH] Add modifying operations --- go.mod | 2 ++ go.sum | 2 ++ main.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index f3da6a6..9c804c9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module modifier go 1.24.1 + +require github.com/Knetic/govaluate v3.0.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bb59ab5 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg= +github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= diff --git a/main.go b/main.go index 83e6b37..fd5621a 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,21 @@ package main import ( + "flag" "fmt" "io" "log" "os" + "path/filepath" + "regexp" + "strings" + + "github.com/Knetic/govaluate" ) var Error *log.Logger var Warning *log.Logger + func init() { log.SetFlags(log.Lmicroseconds | log.Lshortfile) logger := io.MultiWriter(os.Stdout) @@ -23,7 +30,67 @@ func init() { } func main() { - log.Println("Hello, World!") - Warning.Println("Hello, World!") - Error.Println("Hello, World!") -} \ No newline at end of file + flag.Parse() + args := flag.Args() + if len(args) == 0 { + Error.Println("No files provided") + log.Printf("Usage: %s <...files>", os.Args[0]) + log.Printf("Example: %s go run . \"(\\d+)\" \"/10\" Vehicle_JLTV.xml", os.Args[0]) + log.Printf("Note: Field must include valid regex with one capture group") + return + } + field := args[0] + operation := args[1] + files := args[2:] + if field == "" || operation == "" || len(files) == 0 { + Error.Println("Invalid arguments") + log.Printf("Usage: %s <...files>", os.Args[0]) + return + } + log.Printf("Field: %s", field) + log.Printf("Operation: %s", operation) + log.Printf("Processing files: %v", files) + fieldRegex, err := regexp.Compile(field) + if err != nil { + Error.Printf("Error compiling field regex: %v", err) + return + } + + for _, file := range files { + log.Printf("Processing file: %s", file) + fullPath := filepath.Join(".", file) + content, err := os.ReadFile(fullPath) + if err != nil { + Error.Printf("Error reading file: %v", err) + continue + } + lines := strings.Split(string(content), "\n") + for i, line := range lines { + matches := fieldRegex.FindStringSubmatch(line) + if len(matches) > 1 { + log.Printf("Line: %s", line) + oldValue := matches[1] + expression, err := govaluate.NewEvaluableExpression(oldValue + operation) + if err != nil { + Error.Printf("Error creating expression: %v", err) + continue + } + parameters := map[string]interface{}{"oldValue": oldValue} + newValue, err := expression.Evaluate(parameters) + if err != nil { + Error.Printf("Error evaluating expression: %v", err) + continue + } + updatedLine := strings.Replace(line, oldValue, fmt.Sprintf("%v", newValue), 1) + log.Printf("Updated line: %s", updatedLine) + lines[i] = updatedLine + } + } + err = os.WriteFile(fullPath, []byte(strings.Join(lines, "\n")), 0644) + if err != nil { + Error.Printf("Error writing file: %v", err) + continue + } + log.Printf("File %s updated successfully", file) + } +}