updated patch system
This commit is contained in:
parent
bb64cea202
commit
6c9c66bd7a
10 changed files with 434 additions and 208 deletions
95
standard.go
Normal file
95
standard.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// applyStandardModifications applies standard modifications
|
||||
func applyStandardModifications(targetFilePath string, modifications []string) error {
|
||||
// Open the target file
|
||||
targetFile, err := os.Open(targetFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open target file: %v", err)
|
||||
}
|
||||
defer targetFile.Close()
|
||||
|
||||
// Prepare a temporary file for output
|
||||
tempFilePath := targetFilePath + ".tmp"
|
||||
tempFile, err := os.Create(tempFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create temp file: %v", err)
|
||||
}
|
||||
defer tempFile.Close()
|
||||
|
||||
scanner := bufio.NewScanner(targetFile)
|
||||
var targetLines []string
|
||||
for scanner.Scan() {
|
||||
targetLines = append(targetLines, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("failed to read target file: %v", err)
|
||||
}
|
||||
|
||||
// Apply the modifications
|
||||
updatedLines, err := applyModifications(targetLines, modifications)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write the updated lines to the temporary file
|
||||
for _, line := range updatedLines {
|
||||
_, err := fmt.Fprintln(tempFile, line)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to temp file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the original file with the updated file
|
||||
err = os.Rename(tempFilePath, targetFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replace target file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// applyModifications applies the changes from the patch file to the target lines
|
||||
func applyModifications(targetLines, modifications []string) ([]string, error) {
|
||||
var result []string
|
||||
i := 0
|
||||
|
||||
for _, mod := range modifications {
|
||||
switch {
|
||||
case strings.HasPrefix(mod, "-"): // Delete or replace
|
||||
mod = strings.TrimPrefix(mod, "-")
|
||||
for i < len(targetLines) {
|
||||
if strings.TrimSpace(targetLines[i]) == strings.TrimSpace(mod) {
|
||||
i++ // Skip this line (delete)
|
||||
break
|
||||
}
|
||||
result = append(result, targetLines[i])
|
||||
i++
|
||||
}
|
||||
case strings.HasPrefix(mod, "+"): // Add or replace
|
||||
mod = strings.TrimPrefix(mod, "+")
|
||||
result = append(result, mod)
|
||||
default: // Keep existing lines
|
||||
if i < len(targetLines) {
|
||||
result = append(result, targetLines[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Append remaining lines from the original file
|
||||
for i < len(targetLines) {
|
||||
result = append(result, targetLines[i])
|
||||
i++
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue