added windows compatibility
This commit is contained in:
parent
ea4f2edcfd
commit
a6d2d75c45
3 changed files with 93 additions and 78 deletions
35
pref.go
35
pref.go
|
@ -16,6 +16,7 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
|
|||
}
|
||||
defer targetFile.Close()
|
||||
|
||||
// Create a temporary file
|
||||
tempFilePath := targetFilePath + ".tmp"
|
||||
tempFile, err := os.Create(tempFilePath)
|
||||
if err != nil {
|
||||
|
@ -23,30 +24,32 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
|
|||
}
|
||||
defer tempFile.Close()
|
||||
|
||||
scanner := bufio.NewScanner(targetFile)
|
||||
modMap := map[string]string{}
|
||||
|
||||
// Prepare the modification map
|
||||
// Parse modifications into a map for easy lookup
|
||||
modMap := make(map[string]string)
|
||||
for _, mod := range modifications {
|
||||
if strings.HasPrefix(mod, "+pref") {
|
||||
// Strip the `+` and extract the key within parentheses
|
||||
start := strings.Index(mod, "(")
|
||||
end := strings.LastIndex(mod, ")")
|
||||
if start > 0 && end > start {
|
||||
key := strings.TrimSpace(mod[start+1 : end])
|
||||
modMap[key] = strings.TrimPrefix(mod, "+") // Remove the `+` prefix
|
||||
key := strings.TrimSpace(mod[start+1 : end]) // Extract the preference key
|
||||
modMap[key] = mod // Full modification line
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
modifiedLines := map[string]bool{}
|
||||
scanner := bufio.NewScanner(targetFile)
|
||||
modifiedLines := map[string]bool{} // Tracks applied modifications
|
||||
|
||||
// Process each line of the target file
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
lineModified := false
|
||||
|
||||
// Check if the line matches any modification key
|
||||
lineModified := false
|
||||
for key, replacement := range modMap {
|
||||
if strings.Contains(line, key) && !modifiedLines[key] {
|
||||
// Write the replacement line
|
||||
fmt.Printf("Replacing line: %s\n", line)
|
||||
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
|
||||
return fmt.Errorf("failed to write to temp file: %v", err)
|
||||
|
@ -69,21 +72,25 @@ func applyPrefModifications(targetFilePath string, modifications []string) error
|
|||
return fmt.Errorf("failed to read target file: %v", err)
|
||||
}
|
||||
|
||||
// Write remaining modifications (new preferences)
|
||||
// Append remaining modifications that were not in the file
|
||||
for key, replacement := range modMap {
|
||||
if !modifiedLines[key] {
|
||||
// Remove the `+` prefix before writing
|
||||
cleanReplacement := strings.TrimPrefix(replacement, "+")
|
||||
if _, err := tempFile.WriteString(cleanReplacement + "\n"); err != nil {
|
||||
return fmt.Errorf("failed to write new preference to temp file: %v", err)
|
||||
fmt.Printf("Adding new preference: %s\n", replacement)
|
||||
if _, err := tempFile.WriteString(replacement + "\n"); err != nil {
|
||||
return fmt.Errorf("failed to append new preference to temp file: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the original file with the temporary file
|
||||
// Close the files to ensure proper cleanup
|
||||
tempFile.Close()
|
||||
targetFile.Close()
|
||||
|
||||
// Replace the original file with the modified temp file
|
||||
if err := os.Rename(tempFilePath, targetFilePath); err != nil {
|
||||
return fmt.Errorf("failed to replace target file: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Preferences successfully modified.")
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue