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) log.SetOutput(logger) Error = log.New(io.MultiWriter(os.Stderr, os.Stdout), fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"), log.Lmicroseconds|log.Lshortfile) Warning = log.New(os.Stdout, fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"), log.Lmicroseconds|log.Lshortfile) } func main() { 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) } }