added t:new and --patches flag
This commit is contained in:
parent
2fad12ea91
commit
0b44140463
17 changed files with 227 additions and 59 deletions
90
main.go
90
main.go
|
@ -11,8 +11,9 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
// Define the --path flag
|
||||
// Define the --path and --patches flags
|
||||
rootPath := flag.String("path", ".", "Root path for patch application")
|
||||
patchSource := flag.String("patches", "./patches", "File or directory containing patch(es)")
|
||||
flag.Parse()
|
||||
|
||||
// Convert root path to an absolute path
|
||||
|
@ -21,31 +22,43 @@ func main() {
|
|||
log.Fatalf("Failed to resolve absolute path for root: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Starting custom patcher...\nRoot path: %s\n", absoluteRootPath)
|
||||
// Resolve patches path (handle both absolute and relative paths)
|
||||
absolutePatchesPath := *patchSource
|
||||
if !filepath.IsAbs(absolutePatchesPath) {
|
||||
absolutePatchesPath = filepath.Clean(filepath.Join(absoluteRootPath, *patchSource))
|
||||
}
|
||||
|
||||
fmt.Printf("Starting custom patcher...\nRoot path: %s\nPatches source: %s\n", absoluteRootPath, absolutePatchesPath)
|
||||
|
||||
patchDir := "./patches"
|
||||
var successfulPatches, failedPatches []string
|
||||
|
||||
err = filepath.Walk(patchDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".patch") {
|
||||
fmt.Printf("Applying patch: %s\n", path)
|
||||
err := applyPatch(path, absoluteRootPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to apply patch %s: %v\n", path, err)
|
||||
failedPatches = append(failedPatches, path)
|
||||
} else {
|
||||
fmt.Printf("Successfully applied patch: %s\n", path)
|
||||
successfulPatches = append(successfulPatches, path)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Determine if --patches is a directory or a file
|
||||
info, err := os.Stat(absolutePatchesPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading patches: %v", err)
|
||||
log.Fatalf("Failed to access patch source: %v", err)
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
// Walk through directory to apply all patches
|
||||
err = filepath.Walk(absolutePatchesPath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".patch") {
|
||||
applyPatchWrapper(path, absoluteRootPath, &successfulPatches, &failedPatches)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading patches from directory: %v", err)
|
||||
}
|
||||
} else {
|
||||
// Single file provided
|
||||
if strings.HasSuffix(info.Name(), ".patch") {
|
||||
applyPatchWrapper(absolutePatchesPath, absoluteRootPath, &successfulPatches, &failedPatches)
|
||||
} else {
|
||||
log.Fatalf("Provided patch file is not a .patch file")
|
||||
}
|
||||
}
|
||||
|
||||
// Print the summary
|
||||
|
@ -69,6 +82,19 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// applyPatchWrapper wraps the patch application and updates results
|
||||
func applyPatchWrapper(patchPath, rootPath string, successfulPatches, failedPatches *[]string) {
|
||||
fmt.Printf("Applying patch: %s\n", patchPath)
|
||||
err := applyPatch(patchPath, rootPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to apply patch %s: %v\n", patchPath, err)
|
||||
*failedPatches = append(*failedPatches, patchPath)
|
||||
} else {
|
||||
fmt.Printf("Successfully applied patch: %s\n", patchPath)
|
||||
*successfulPatches = append(*successfulPatches, patchPath)
|
||||
}
|
||||
}
|
||||
|
||||
// applyPatch processes a single patch file
|
||||
func applyPatch(patchPath, rootPath string) error {
|
||||
file, err := os.Open(patchPath)
|
||||
|
@ -120,14 +146,20 @@ func applyPatch(patchPath, rootPath string) error {
|
|||
return fmt.Errorf("failed to read patch file: %v", err)
|
||||
}
|
||||
|
||||
if inputFilePath == "" || outputFilePath == "" {
|
||||
return fmt.Errorf("patch file must specify both input (i:) and output (o:) files")
|
||||
if inputFilePath == "" && patchType != "new" {
|
||||
return fmt.Errorf("patch file must specify input (i:) file")
|
||||
}
|
||||
|
||||
// Replace the original file with the temporary file
|
||||
err = os.Rename(inputFilePath, outputFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replace output file: %v", err)
|
||||
if outputFilePath == "" {
|
||||
return fmt.Errorf("patch file must specify output (o:) file")
|
||||
}
|
||||
|
||||
if patchType != "new" {
|
||||
// Replace the original file with the temporary file
|
||||
err = os.Rename(inputFilePath, outputFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replace output file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Process based on patch type
|
||||
|
@ -136,6 +168,8 @@ func applyPatch(patchPath, rootPath string) error {
|
|||
return applyPrefModifications(outputFilePath, modifications)
|
||||
case "standard":
|
||||
return applyStandardModifications(outputFilePath, modifications)
|
||||
case "new":
|
||||
return applyNewModifications(outputFilePath, modifications)
|
||||
default:
|
||||
fmt.Printf("Type not specified defaulting to standard")
|
||||
return applyStandardModifications(outputFilePath, modifications)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue